MCPcopy Create free account
hub / github.com/apple/ml-simplefold / InferenceWrapper

Class InferenceWrapper

src/simplefold/wrapper.py:199–392  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

197
198
199class InferenceWrapper:
200 def __init__(
201 self,
202 output_dir,
203 prediction_dir,
204 num_steps,
205 nsample_per_protein,
206 tau,
207 device,
208 backend,
209 ):
210 self.num_steps = num_steps
211 self.nsample_per_protein = nsample_per_protein
212 self.tau = tau
213 self.device = device
214 self.backend = backend
215
216 if self.backend == "mlx" and not MLX_AVAILABLE:
217 self.backend = "torch"
218 print("MLX not installed, switch to torch backend.")
219
220 # create output directory
221 output_dir = Path(output_dir)
222 output_dir.mkdir(parents=True, exist_ok=True)
223
224 # create cache directory
225 cache = output_dir / "cache"
226 cache.mkdir(parents=True, exist_ok=True)
227
228 # create prediction directory
229 prediction_dir = output_dir / prediction_dir
230 prediction_dir.mkdir(parents=True, exist_ok=True)
231
232 self.output_dir = output_dir
233 self.cache = cache
234 self.prediction_dir = prediction_dir
235
236 self.initialize_esm_model()
237 self.initialize_others()
238
239 def initialize_esm_model(self):
240 # load ESM2 model
241 esm_model, esm_dict = esm_registry["esm2_3B"]()
242 af2_to_esm = _af2_to_esm(esm_dict)
243
244 if self.backend == "torch":
245 esm_model = esm_model.to(self.device)
246 af2_to_esm = af2_to_esm.to(self.device)
247 elif self.backend == "mlx":
248 esm_model_mlx = ESM2MLX(num_layers=36, embed_dim=2560, attention_heads=40)
249 esm_state_dict_torch = esm_model.cpu().state_dict()
250
251 esm_state_dict_torch = {
252 k: mx.array(v)
253 for k, v in starmap(map_torch_to_mlx, esm_state_dict_torch.items())
254 if k is not None
255 }
256 esm_model_mlx.update(tree_unflatten(list(esm_state_dict_torch.items())))

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected