cyrusyc commited on
Commit
2d8bda8
1 Parent(s): 5cb0b67

test hf hub

Browse files
mlip_arena/models/__init__.py CHANGED
@@ -27,19 +27,42 @@ class MLIP(
27
  super().__init__(*args, **kwargs)
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  class MLIPCalculator(Calculator):
 
 
 
 
 
31
  def __init__(
32
  self,
 
33
  model_path: str | Path,
34
  device: torch.device | None = None,
 
 
 
 
 
35
  ):
36
- super().__init__()
37
- self.name: str = self.__class__.__name__
38
- self.device = device or torch.device(
39
- "cuda" if torch.cuda.is_available() else "cpu"
40
- )
41
- self.model: MLIP = MLIP.from_pretrained(model_path, map_location=self.device)
42
- self.implemented_properties = ["energy", "forces", "stress"]
43
 
44
  def calculate(
45
  self, atoms: Atoms, properties: list[str], system_changes: list = all_changes
 
27
  super().__init__(*args, **kwargs)
28
 
29
 
30
+ class ModuleMLIP(MLIP):
31
+ def __init__(self, model: nn.Module, *args, **kwargs) -> None:
32
+ super().__init__(*args, **kwargs)
33
+ self.register_module("model", model)
34
+
35
+ def forward(self, x):
36
+ print("Forwarding...")
37
+ out = self.model(x)
38
+ print("Forwarded!")
39
+ return out
40
+
41
+
42
  class MLIPCalculator(Calculator):
43
+ name: str
44
+ device: torch.device
45
+ model: MLIP
46
+ implemented_properties: list[str] = ["energy", "forces", "stress"]
47
+
48
  def __init__(
49
  self,
50
+ # PyTorch
51
  model_path: str | Path,
52
  device: torch.device | None = None,
53
+ # ASE Calculator
54
+ restart=None,
55
+ atoms=None,
56
+ directory=".",
57
+ **kwargs,
58
  ):
59
+ super().__init__(restart=restart, atoms=atoms, directory=directory, **kwargs)
60
+ # self.name: str = self.__class__.__name__
61
+ # self.device = device or torch.device(
62
+ # "cuda" if torch.cuda.is_available() else "cpu"
63
+ # )
64
+ # self.model: MLIP = MLIP.from_pretrained(model_path, map_location=self.device)
65
+ # self.implemented_properties = ["energy", "forces", "stress"]
66
 
67
  def calculate(
68
  self, atoms: Atoms, properties: list[str], system_changes: list = all_changes
mlip_arena/models/mace.py CHANGED
@@ -4,20 +4,54 @@ from ase.calculators.calculator import all_changes
4
  from huggingface_hub import hf_hub_download
5
  from torch_geometric.data import Data
6
 
7
- from mlip_arena.models import MLIPCalculator
8
 
9
 
10
  class MACE_MP_Medium(MLIPCalculator):
11
- def __init__(self, device: torch.device | None = None):
 
 
 
 
 
 
 
 
 
 
 
12
  fpath = hf_hub_download(
13
  repo_id="cyrusyc/mace-universal",
14
  subfolder="pretrained",
15
  filename="2023-12-12-mace-128-L1_epoch-199.model",
16
  revision=None, # TODO: Add revision
17
  )
18
- super().__init__(model_path=fpath, device=device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- self.name = "MACE-MP-0 (medium)"
21
  self.version = "1.0.0"
22
  self.implemented_properties = [
23
  "energy",
 
4
  from huggingface_hub import hf_hub_download
5
  from torch_geometric.data import Data
6
 
7
+ from mlip_arena.models import MLIP, MLIPCalculator, ModuleMLIP
8
 
9
 
10
  class MACE_MP_Medium(MLIPCalculator):
11
+ def __init__(
12
+ self,
13
+ device: torch.device | None = None,
14
+ restart=None,
15
+ atoms=None,
16
+ directory=".",
17
+ **kwargs,
18
+ ):
19
+ # Download the pytorch model from huggingface to local and load it
20
+ # NOTE: this is not the ideal way to load the model, but it is the simplest
21
+ # way to do it for now. Ideally, if the model is the subclass of PyTorchModelHubMixin,
22
+ # we should be able to load it directly from the hub or local using MLIP class.
23
  fpath = hf_hub_download(
24
  repo_id="cyrusyc/mace-universal",
25
  subfolder="pretrained",
26
  filename="2023-12-12-mace-128-L1_epoch-199.model",
27
  revision=None, # TODO: Add revision
28
  )
29
+ # module = ModuleMLIP(torch.load(fpath, map_location="cpu"))
30
+ print(torch.load(fpath, map_location="cpu"))
31
+ repo_id = f"atomind/{self.__class__.__name__}".replace("_", "-")
32
+ # module.save_pretrained(
33
+ # save_directory=self.__class__.__name__,
34
+ # repo_id=repo_id,
35
+ # push_to_hub=True,
36
+ # )
37
+
38
+ super().__init__(
39
+ model_path=repo_id,
40
+ device=device,
41
+ restart=restart,
42
+ atoms=atoms,
43
+ directory=directory,
44
+ **kwargs,
45
+ )
46
+
47
+ # self.name: str = self.__class__.__name__
48
+ # self.device = device or torch.device(
49
+ # "cuda" if torch.cuda.is_available() else "cpu"
50
+ # )
51
+ # self.model: MLIP = ModuleMLIP.from_pretrained(repo_id, map_location=self.device)
52
+ # self.implemented_properties = ["energy", "forces", "stress"]
53
 
54
+ self.display = "MACE-MP-0 (medium)"
55
  self.version = "1.0.0"
56
  self.implemented_properties = [
57
  "energy",
pyproject.toml CHANGED
@@ -31,6 +31,7 @@ dependencies=[
31
  "torch_dftd>=0.4.0",
32
  "huggingface_hub",
33
  "torch-geometric>=2.5.2",
 
34
  ]
35
 
36
  [project.urls]
 
31
  "torch_dftd>=0.4.0",
32
  "huggingface_hub",
33
  "torch-geometric>=2.5.2",
34
+ "safetensors"
35
  ]
36
 
37
  [project.urls]
tests/hf_hub.ipynb ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import torch\n",
10
+ "from huggingface_hub import hf_hub_download\n",
11
+ "from mlip_arena.models import MLIP, MLIPCalculator, ModuleMLIP"
12
+ ]
13
+ },
14
+ {
15
+ "cell_type": "code",
16
+ "execution_count": 3,
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "\n",
21
+ "fpath = hf_hub_download(\n",
22
+ " repo_id=\"cyrusyc/mace-universal\",\n",
23
+ " subfolder=\"pretrained\",\n",
24
+ " filename=\"2023-12-12-mace-128-L1_epoch-199.model\",\n",
25
+ " revision=None, # TODO: Add revision\n",
26
+ ")\n",
27
+ "\n",
28
+ "model = torch.load(fpath, map_location=\"cpu\")"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 5,
34
+ "metadata": {},
35
+ "outputs": [],
36
+ "source": [
37
+ "module = ModuleMLIP(model=model)"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "code",
42
+ "execution_count": 12,
43
+ "metadata": {},
44
+ "outputs": [
45
+ {
46
+ "name": "stderr",
47
+ "output_type": "stream",
48
+ "text": [
49
+ "model.safetensors: 100%|██████████| 44.2M/44.2M [00:02<00:00, 20.6MB/s]\n"
50
+ ]
51
+ },
52
+ {
53
+ "data": {
54
+ "text/plain": [
55
+ "CommitInfo(commit_url='https://huggingface.co/atomind/mace-mp-medium/commit/ef94f6bd9c7167bb28d594d5a3e7a5fbfbda2acb', commit_message='Push model using huggingface_hub.', commit_description='', oid='ef94f6bd9c7167bb28d594d5a3e7a5fbfbda2acb', pr_url=None, pr_revision=None, pr_num=None)"
56
+ ]
57
+ },
58
+ "execution_count": 12,
59
+ "metadata": {},
60
+ "output_type": "execute_result"
61
+ }
62
+ ],
63
+ "source": [
64
+ "module.save_pretrained(\n",
65
+ " \"mace\",\n",
66
+ " repo_id=\"atomind/MACE_MP_Medium\".lower().replace(\"_\", \"-\"),\n",
67
+ " push_to_hub=True\n",
68
+ ")"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "code",
73
+ "execution_count": null,
74
+ "metadata": {},
75
+ "outputs": [],
76
+ "source": []
77
+ }
78
+ ],
79
+ "metadata": {
80
+ "kernelspec": {
81
+ "display_name": "Python 3",
82
+ "language": "python",
83
+ "name": "python3"
84
+ },
85
+ "language_info": {
86
+ "codemirror_mode": {
87
+ "name": "ipython",
88
+ "version": 3
89
+ },
90
+ "file_extension": ".py",
91
+ "mimetype": "text/x-python",
92
+ "name": "python",
93
+ "nbconvert_exporter": "python",
94
+ "pygments_lexer": "ipython3",
95
+ "version": "3.11.8"
96
+ }
97
+ },
98
+ "nbformat": 4,
99
+ "nbformat_minor": 2
100
+ }