Skip to content
Open
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
99 changes: 50 additions & 49 deletions cgcnn2/cli/cgcnn_ft.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,8 @@ def main():
)
model_args = argparse.Namespace(**checkpoint["args"])

# Prepare dataset and infer feature dimensions
if args.full_set:
sample_set = args.full_set
else:
sample_set = args.train_set

dataset = CIFData(sample_set)
atom_graph, _, _ = dataset[0]
# Infer feature dimensions from the first training sample
atom_graph, _, _ = train_dataset[0]
orig_atom_fea_len = atom_graph[0].shape[-1]
nbr_fea_len = atom_graph[1].shape[-1]

Expand All @@ -330,6 +324,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)

valid_loader = DataLoader(
Expand All @@ -339,6 +334,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)

test_loader = DataLoader(
Expand All @@ -348,6 +344,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)

if args.train_last_fc:
Expand All @@ -358,9 +355,7 @@ def main():
if args.reset:
logging.info("The last fully connected layer will be reset.")
# Reset the fully connected layers after graph features were obtained
model.fc_out = nn.Linear(model.fc_out.in_features, 1)
if args.device.type == "cuda":
model.fc_out = model.fc_out.cuda()
model.fc_out = nn.Linear(model.fc_out.in_features, 1).to(args.device)

# Define parameters to be fine-tuned
fc_parameters = [param for param in model.fc_out.parameters()]
Expand All @@ -375,23 +370,17 @@ def main():
logging.info("All the fully connected layers will be reset.")
model.conv_to_fc = nn.Linear(
model.conv_to_fc.in_features, model.conv_to_fc.out_features
)
if args.device.type == "cuda":
model.conv_to_fc = model.conv_to_fc.cuda()
).to(args.device)

if hasattr(model, "fcs"):
model.fcs = nn.ModuleList(
[
nn.Linear(layer.in_features, layer.out_features)
for layer in model.fcs
]
)
if args.device.type == "cuda":
model.fcs = nn.ModuleList([layer.cuda() for layer in model.fcs])
).to(args.device)

model.fc_out = nn.Linear(model.fc_out.in_features, 1)
if args.device.type == "cuda":
model.fc_out = model.fc_out.cuda()
model.fc_out = nn.Linear(model.fc_out.in_features, 1).to(args.device)

# Define parameters to be trained
fc_parameters = [param for param in model.conv_to_fc.parameters()]
Expand All @@ -412,9 +401,15 @@ def main():
[
{"params": fc_parameters, "lr": args.lr_fc},
{"params": other_parameters, "lr": args.lr_non_fc},
]
],
fused=args.device.type == "cuda",
)

# Compile the model on GPU for faster training
if args.device.type == "cuda":
model.compile()
amp_enabled = args.device.type == "cuda"

# Initialize the scheduler
scheduler: ReduceLROnPlateau | None = None

Expand Down Expand Up @@ -450,21 +445,24 @@ def main():
train_loss = 0.0
for input_data, targets, _ in train_loader:
atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx = input_data
atom_fea = atom_fea.to(args.device)
nbr_fea = nbr_fea.to(args.device)
nbr_fea_idx = nbr_fea_idx.to(args.device)
crystal_atom_idx = crystal_atom_idx.to(args.device)
targets = targets.to(args.device)
atom_fea = atom_fea.to(args.device, non_blocking=True)
nbr_fea = nbr_fea.to(args.device, non_blocking=True)
nbr_fea_idx = nbr_fea_idx.to(args.device, non_blocking=True)
crystal_atom_idx = crystal_atom_idx.to(args.device, non_blocking=True)
targets = targets.to(args.device, non_blocking=True)

# Forward pass
outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)
if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature).to(args.device)
loss = (loss * bias).mean()
else:
loss = loss.mean()
with torch.autocast(
args.device.type, dtype=torch.bfloat16, enabled=amp_enabled
):
outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)
if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature)
loss = (loss * bias).mean()
else:
loss = loss.mean()

optimizer.zero_grad()
loss.backward()
Expand All @@ -481,21 +479,24 @@ def main():
with torch.inference_mode():
for input_data, targets, _ in valid_loader:
atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx = input_data
atom_fea = atom_fea.to(args.device)
nbr_fea = nbr_fea.to(args.device)
nbr_fea_idx = nbr_fea_idx.to(args.device)
crystal_atom_idx = crystal_atom_idx.to(args.device)
targets = targets.to(args.device)

outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)

if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature).to(args.device)
loss = (loss * bias).mean()
else:
loss = loss.mean()
atom_fea = atom_fea.to(args.device, non_blocking=True)
nbr_fea = nbr_fea.to(args.device, non_blocking=True)
nbr_fea_idx = nbr_fea_idx.to(args.device, non_blocking=True)
crystal_atom_idx = crystal_atom_idx.to(args.device, non_blocking=True)
targets = targets.to(args.device, non_blocking=True)

with torch.autocast(
args.device.type, dtype=torch.bfloat16, enabled=amp_enabled
):
outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)

if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature)
loss = (loss * bias).mean()
else:
loss = loss.mean()

valid_loss += loss.item()

Expand Down
1 change: 1 addition & 0 deletions cgcnn2/cli/cgcnn_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=(args.device.type == "cuda"),
persistent_workers=args.workers > 0,
)

# Initialize and load model
Expand Down
76 changes: 47 additions & 29 deletions cgcnn2/cli/cgcnn_tr.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ def main():
# Move to device
model.to(args.device)

# Compile the model on GPU for faster training
if args.device.type == "cuda":
model.compile()
amp_enabled = args.device.type == "cuda"

# Dataloaders
train_loader = DataLoader(
train_dataset,
Expand All @@ -302,6 +307,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)
valid_loader = DataLoader(
valid_dataset,
Expand All @@ -310,6 +316,7 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)
test_loader = DataLoader(
test_dataset,
Expand All @@ -318,10 +325,15 @@ def main():
num_workers=args.workers,
collate_fn=collate_pool,
pin_memory=args.device.type == "cuda",
persistent_workers=args.workers > 0,
)

# Single LR optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)
optimizer = torch.optim.Adam(
model.parameters(),
lr=args.learning_rate,
fused=args.device.type == "cuda",
)

# Optional LR scheduler
scheduler = None
Expand Down Expand Up @@ -350,23 +362,26 @@ def main():
train_loss_sum = 0.0
for input_data, targets, _ in train_loader:
atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx = input_data
atom_fea = atom_fea.to(args.device)
nbr_fea = nbr_fea.to(args.device)
nbr_fea_idx = nbr_fea_idx.to(args.device)
crystal_atom_idx = crystal_atom_idx.to(args.device)
targets = targets.to(args.device)
atom_fea = atom_fea.to(args.device, non_blocking=True)
nbr_fea = nbr_fea.to(args.device, non_blocking=True)
nbr_fea_idx = nbr_fea_idx.to(args.device, non_blocking=True)
crystal_atom_idx = crystal_atom_idx.to(args.device, non_blocking=True)
targets = targets.to(args.device, non_blocking=True)

optimizer.zero_grad()

outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)
with torch.autocast(
args.device.type, dtype=torch.bfloat16, enabled=amp_enabled
):
outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)

if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature).to(args.device)
loss = (loss * bias).mean()
else:
loss = loss.mean()
if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature)
loss = (loss * bias).mean()
else:
loss = loss.mean()

loss.backward()
optimizer.step()
Expand All @@ -383,21 +398,24 @@ def main():
with torch.inference_mode():
for input_data, targets, _ in valid_loader:
atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx = input_data
atom_fea = atom_fea.to(args.device)
nbr_fea = nbr_fea.to(args.device)
nbr_fea_idx = nbr_fea_idx.to(args.device)
crystal_atom_idx = crystal_atom_idx.to(args.device)
targets = targets.to(args.device)

outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)

if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature).to(args.device)
loss = (loss * bias).mean()
else:
loss = loss.mean()
atom_fea = atom_fea.to(args.device, non_blocking=True)
nbr_fea = nbr_fea.to(args.device, non_blocking=True)
nbr_fea_idx = nbr_fea_idx.to(args.device, non_blocking=True)
crystal_atom_idx = crystal_atom_idx.to(args.device, non_blocking=True)
targets = targets.to(args.device, non_blocking=True)

with torch.autocast(
args.device.type, dtype=torch.bfloat16, enabled=amp_enabled
):
outputs, _ = model(atom_fea, nbr_fea, nbr_fea_idx, crystal_atom_idx)
loss = criterion(outputs, targets)

if args.bias_temperature > 0.0:
# Boltzmann factor weighting
bias = torch.exp(-targets / args.bias_temperature)
loss = (loss * bias).mean()
else:
loss = loss.mean()

valid_loss_sum += loss.item()

Expand Down
Loading