-
Notifications
You must be signed in to change notification settings - Fork 1
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
[WIP] Add PyG-based GAT implementation. #67
Open
kaminow
wants to merge
14
commits into
main
Choose a base branch
from
dgl-to-pyg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16a32ff
Add PyG-based GAT implementation.
kaminow 8b6683d
Add PyG GAT to config.
kaminow ace257f
Update model_type.
kaminow b81ec61
Migrate PyG-GAT -> GAT.
kaminow 6ab110a
Update GAT tests.
kaminow 30a16f2
Fix README usage.
kaminow a98c0a0
Typo
kaminow 7c8cddf
Update to PyG example.
kaminow 71d1d8e
Remove dgl deps.
kaminow 01d12ca
Featurize SMILES with rdkit.
kaminow 97eb662
Remove irrelevant tests and fix some config args.
kaminow a9352e4
Add side-by-side PyG and DGL versions.
kaminow 9a9f1f6
Add configs for both GAT versions.
kaminow cfc66dc
Add option to use GATv2Conv.
kaminow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,5 @@ dependencies: | |
- numpy | ||
- h5py | ||
- e3nn | ||
- dgllife | ||
- dgl | ||
- rdkit | ||
- ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,6 @@ dependencies: | |
- numpy | ||
- h5py | ||
- e3nn | ||
- dgllife | ||
- dgl | ||
- rdkit | ||
- ase | ||
- fsspec | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,6 @@ dependencies: | |
- numpy | ||
- h5py | ||
- e3nn | ||
- dgllife | ||
- dgl | ||
- rdkit | ||
- ase | ||
- pydantic >=1.10.8,<2.0.0a0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,5 @@ dependencies: | |
- numpy | ||
- h5py | ||
- e3nn | ||
- dgllife | ||
- dgl | ||
- rdkit | ||
- rdkit | ||
- ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,5 @@ dependencies: | |
- numpy | ||
- h5py | ||
- e3nn | ||
- dgllife | ||
- dgl | ||
- rdkit | ||
- ase | ||
- ase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,8 @@ class ModelType(StringEnum): | |
""" | ||
|
||
GAT = "GAT" | ||
pyg_gat = "pyg_gat" | ||
dgl_gat = "dgl_gat" | ||
schnet = "schnet" | ||
e3nn = "e3nn" | ||
visnet = "visnet" | ||
|
@@ -403,6 +405,127 @@ def _check_grouped(values): | |
|
||
|
||
class GATModelConfig(ModelConfigBase): | ||
""" | ||
Class for constructing a GAT ML model. Default values here are based on the values | ||
in DGL-LifeSci. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DGL-LifeSci gone now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the defaults are still based on the defaults in that package, even though we're not using their code anymore |
||
""" | ||
|
||
model_type: ModelType = Field(ModelType.GAT, const=True) | ||
|
||
in_channels: int = Field( | ||
-1, | ||
description=( | ||
"Input size. Can be left as -1 (default) to interpret based on " | ||
"first forward call." | ||
), | ||
) | ||
hidden_channels: int = Field(32, description="Hidden embedding size.") | ||
num_layers: int = Field(2, description="Number of GAT layers.") | ||
v2: bool = Field(False, description="Use GATv2Conv layer instead of GATConv.") | ||
dropout: float = Field(0, description="Dropout probability.") | ||
heads: int = Field(4, description="Number of attention heads for each GAT layer.") | ||
negative_slope: float = Field( | ||
0.2, description="LeakyReLU angle of the negative slope." | ||
) | ||
|
||
def _build(self, mtenn_params={}): | ||
""" | ||
Build an ``mtenn`` GAT ``Model`` from this config. | ||
|
||
:meta public: | ||
|
||
Parameters | ||
---------- | ||
mtenn_params : dict, optional | ||
Dictionary that stores the ``Readout`` objects for the individual | ||
predictions and for the combined prediction, and the ``Combination`` object | ||
in the case of a multi-pose model. These are all constructed the same for all | ||
``Model`` types, so we can just handle them in the base class. Keys in the | ||
dict will be: | ||
|
||
* "combination": :py:mod:`Combination <mtenn.combination>` | ||
|
||
* "pred_readout": :py:mod:`Readout <mtenn.readout>` for individual | ||
pose predictions | ||
|
||
* "comb_readout": :py:mod:`Readout <mtenn.readout>` for combined | ||
prediction (in the case of a multi-pose model) | ||
|
||
although the combination-related entries will be ignore because this is a | ||
ligand-only model. | ||
|
||
Returns | ||
------- | ||
mtenn.model.Model | ||
Model constructed from the config | ||
""" | ||
from mtenn.conversion_utils.gat import GAT | ||
|
||
model = GAT( | ||
in_channels=self.in_channels, | ||
hidden_channels=self.hidden_channels, | ||
num_layers=self.num_layers, | ||
v2=self.v2, | ||
dropout=self.dropout, | ||
heads=self.heads, | ||
negative_slope=self.negative_slope, | ||
) | ||
|
||
pred_readout = mtenn_params.get("pred_readout", None) | ||
return GAT.get_model(model=model, pred_readout=pred_readout, fix_device=True) | ||
|
||
|
||
class PyGGATModelConfig(GATModelConfig): | ||
model_type: ModelType = Field(ModelType.pyg_gat, const=True) | ||
|
||
def _build(self, mtenn_params={}): | ||
""" | ||
Build an ``mtenn`` PyGGAT ``Model`` from this config. | ||
|
||
:meta public: | ||
|
||
Parameters | ||
---------- | ||
mtenn_params : dict, optional | ||
Dictionary that stores the ``Readout`` objects for the individual | ||
predictions and for the combined prediction, and the ``Combination`` object | ||
in the case of a multi-pose model. These are all constructed the same for all | ||
``Model`` types, so we can just handle them in the base class. Keys in the | ||
dict will be: | ||
|
||
* "combination": :py:mod:`Combination <mtenn.combination>` | ||
|
||
* "pred_readout": :py:mod:`Readout <mtenn.readout>` for individual | ||
pose predictions | ||
|
||
* "comb_readout": :py:mod:`Readout <mtenn.readout>` for combined | ||
prediction (in the case of a multi-pose model) | ||
|
||
although the combination-related entries will be ignore because this is a | ||
ligand-only model. | ||
|
||
Returns | ||
------- | ||
mtenn.model.Model | ||
Model constructed from the config | ||
""" | ||
from mtenn.conversion_utils.pyg_gat import PyGGAT | ||
|
||
model = PyGGAT( | ||
in_channels=self.in_channels, | ||
hidden_channels=self.hidden_channels, | ||
num_layers=self.num_layers, | ||
v2=self.v2, | ||
dropout=self.dropout, | ||
heads=self.heads, | ||
negative_slope=self.negative_slope, | ||
) | ||
|
||
pred_readout = mtenn_params.get("pred_readout", None) | ||
return PyGGAT.get_model(model=model, pred_readout=pred_readout, fix_device=True) | ||
|
||
|
||
class DGLGATModelConfig(ModelConfigBase): | ||
""" | ||
Class for constructing a graph attention ML model. Note that there are two methods | ||
for defining the size of the model: | ||
|
@@ -440,7 +563,7 @@ class GATModelConfig(ModelConfigBase): | |
"biases": bool, | ||
} #: :meta private: | ||
|
||
model_type: ModelType = Field(ModelType.GAT, const=True) | ||
model_type: ModelType = Field(ModelType.dgl_gat, const=True) | ||
|
||
in_feats: int = Field( | ||
_CanonicalAtomFeaturizer().feat_size(), | ||
|
@@ -532,7 +655,7 @@ class GATModelConfig(ModelConfigBase): | |
_from_num_layers = False | ||
|
||
@root_validator(pre=False) | ||
def massage_into_lists(cls, values) -> GATModelConfig: | ||
def massage_into_lists(cls, values) -> DGLGATModelConfig: | ||
""" | ||
Validator to handle unifying all the values into the proper list forms based on | ||
the rules described in the class docstring. | ||
|
@@ -621,9 +744,9 @@ def _build(self, mtenn_params={}): | |
mtenn.model.Model | ||
Model constructed from the config | ||
""" | ||
from mtenn.conversion_utils.gat import GAT | ||
from mtenn.conversion_utils.dgl_gat import DGLGAT | ||
|
||
model = GAT( | ||
model = DGLGAT( | ||
in_feats=self.in_feats, | ||
hidden_feats=self.hidden_feats, | ||
num_heads=self.num_heads, | ||
|
@@ -638,9 +761,9 @@ def _build(self, mtenn_params={}): | |
) | ||
|
||
pred_readout = mtenn_params.get("pred_readout", None) | ||
return GAT.get_model(model=model, pred_readout=pred_readout, fix_device=True) | ||
return DGLGAT.get_model(model=model, pred_readout=pred_readout, fix_device=True) | ||
|
||
def _update(self, config_updates={}) -> GATModelConfig: | ||
def _update(self, config_updates={}) -> DGLGATModelConfig: | ||
""" | ||
GAT-specific implementation of updating logic. Need to handle stuff specially | ||
to make sure that the original method of specifying parameters (either from a | ||
|
@@ -656,15 +779,15 @@ def _update(self, config_updates={}) -> GATModelConfig: | |
|
||
Returns | ||
------- | ||
GATModelConfig | ||
New ``GATModelConfig`` object | ||
DGLGATModelConfig | ||
New ``DGLGATModelConfig`` object | ||
""" | ||
orig_config = self.dict() | ||
if self._from_num_layers or ("num_layers" in config_updates): | ||
# If originally generated from num_layers, want to pull out the first entry | ||
# in each list param so it can be re-broadcast with (potentially) new | ||
# num_layers | ||
for param_name in GATModelConfig.LIST_PARAMS.keys(): | ||
for param_name in DGLGATModelConfig.LIST_PARAMS.keys(): | ||
orig_config[param_name] = orig_config[param_name][0] | ||
|
||
# Get new config by overwriting old stuff with any new stuff | ||
|
@@ -676,7 +799,7 @@ def _update(self, config_updates={}) -> GATModelConfig: | |
): | ||
new_config["activations"] = None | ||
|
||
return GATModelConfig(**new_config) | ||
return DGLGATModelConfig(**new_config) | ||
|
||
|
||
class SchNetModelConfig(ModelConfigBase): | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a convenience function to do this easily for user, easy to mess up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added one in
asapdiscovery
for us to use, but since there's no one right way to featurize a molecule I didn't want to add anything opinionated in here