bayesianflow-for-chem 1.2.1__tar.gz → 1.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of bayesianflow-for-chem might be problematic. Click here for more details.

Files changed (18) hide show
  1. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/PKG-INFO +1 -1
  2. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/__init__.py +1 -1
  3. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/tool.py +64 -2
  4. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem.egg-info/PKG-INFO +1 -1
  5. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/LICENSE +0 -0
  6. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/README.md +0 -0
  7. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/data.py +0 -0
  8. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/model.py +0 -0
  9. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/scorer.py +0 -0
  10. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/train.py +0 -0
  11. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem/vocab.txt +0 -0
  12. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem.egg-info/SOURCES.txt +0 -0
  13. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem.egg-info/dependency_links.txt +0 -0
  14. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem.egg-info/requires.txt +0 -0
  15. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/bayesianflow_for_chem.egg-info/top_level.txt +0 -0
  16. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/pyproject.toml +0 -0
  17. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/setup.cfg +0 -0
  18. {bayesianflow_for_chem-1.2.1 → bayesianflow_for_chem-1.2.2}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: bayesianflow_for_chem
3
- Version: 1.2.1
3
+ Version: 1.2.2
4
4
  Summary: Bayesian flow network framework for Chemistry
5
5
  Home-page: https://augus1999.github.io/bayesian-flow-network-for-chemistry/
6
6
  Author: Nianze A. Tao
@@ -7,5 +7,5 @@ from . import data, tool, train, scorer
7
7
  from .model import ChemBFN, MLP
8
8
 
9
9
  __all__ = ["data", "tool", "train", "scorer", "ChemBFN", "MLP"]
10
- __version__ = "1.2.1"
10
+ __version__ = "1.2.2"
11
11
  __author__ = "Nianze A. Tao (Omozawa Sueno)"
@@ -12,6 +12,11 @@ import torch
12
12
  import numpy as np
13
13
  from torch import cuda, Tensor, softmax
14
14
  from torch.utils.data import DataLoader
15
+ from torch.ao.quantization.quantize_pt2e import prepare_pt2e, convert_pt2e
16
+ from torch.ao.quantization.quantizer.xnnpack_quantizer import (
17
+ XNNPACKQuantizer,
18
+ get_symmetric_quantization_config,
19
+ )
15
20
  from rdkit.Chem import rdDetermineBonds, Bond, MolFromXYZBlock, CanonicalRankAtoms
16
21
  from rdkit.Chem.Scaffolds.MurckoScaffold import MurckoScaffoldSmiles # type: ignore
17
22
  from sklearn.metrics import (
@@ -380,7 +385,10 @@ def sample(
380
385
  assert method.split(":")[0].lower() in ("ode", "bfn")
381
386
  if device is None:
382
387
  device = _find_device()
383
- model.to(device).eval()
388
+ model.to(device)
389
+ if not isinstance(model, torch.fx.GraphModule):
390
+ model.eval() # Calling eval() is not supported for GraphModule
391
+ # model.to(device).eval()
384
392
  if y is not None:
385
393
  y = y.to(device)
386
394
  if isinstance(allowed_tokens, list):
@@ -455,7 +463,10 @@ def inpaint(
455
463
  assert method.split(":")[0].lower() in ("ode", "bfn")
456
464
  if device is None:
457
465
  device = _find_device()
458
- model.to(device).eval()
466
+ model.to(device)
467
+ if not isinstance(model, torch.fx.GraphModule):
468
+ model.eval() # Calling eval() is not supported for GraphModule
469
+ # model.to(device).eval()
459
470
  x = x.to(device)
460
471
  if y is not None:
461
472
  y = y.to(device)
@@ -484,3 +495,54 @@ def inpaint(
484
495
  .replace("<pad>", "")
485
496
  for j in tokens
486
497
  ]
498
+
499
+
500
+ def quantise_model(
501
+ model: ChemBFN, dataloader: DataLoader, mlp: Optional[MLP] = None
502
+ ) -> torch.fx.GraphModule:
503
+ """
504
+ Static quantisation of the input model.
505
+
506
+ :param model: trained ChemBFN model
507
+ :param dataloader: DataLoader instance containing example data for calibration
508
+ :param mlp: trained MLP model (guidance) if applied
509
+ :type model: bayesianflow_for_chem.model.ChemBFN
510
+ :type dataloader: torch.utils.data.DataLoader
511
+ :type mlp: bayesianflow_for_chem.model.MLP | None
512
+ :return: quantised model
513
+ :rtype: torch.fx.GraphModule
514
+ """
515
+ nb, nt = dataloader._get_iterator()._next_data()["token"].shape
516
+ x = 2 * softmax(torch.rand((nb, nt, model.K)), -1) - 1
517
+ t = torch.rand((nb, 1, 1))
518
+ y = torch.randn(nb, 1, model.embedding.weight.shape[0]) if mlp is not None else None
519
+ example_input = (2 * x - 1, t, None, y)
520
+ graph_model = torch.export.export_for_training(model, example_input).module()
521
+ quantizer = XNNPACKQuantizer().set_global(get_symmetric_quantization_config())
522
+ prepared_model = prepare_pt2e(graph_model, quantizer)
523
+ # ------- calibration -------
524
+ with torch.inference_mode():
525
+ for data in dataloader:
526
+ x = data["token"]
527
+ if x.shape[0] != nb:
528
+ break
529
+ if mlp is not None:
530
+ y = mlp(data["value"])[:, None, :]
531
+ else:
532
+ y = None
533
+ t = torch.rand((x.shape[0], 1, 1))
534
+ beta = model.calc_beta(t)
535
+ e_x = torch.nn.functional.one_hot(x, model.K).float()
536
+ mu = beta * (model.K * e_x - 1)
537
+ sigma = (beta * model.K).sqrt()
538
+ theta = softmax(mu + sigma * torch.randn_like(mu), -1)
539
+ prepared_model(2 * theta - 1, t, None, y)
540
+ quantised_model = convert_pt2e(prepared_model)
541
+ quantised_model = torch.export.export_for_training(
542
+ quantised_model, example_input
543
+ ).module() # remove the weights of original model
544
+ quantised_model.sample = model.sample
545
+ quantised_model.ode_sample = model.ode_sample
546
+ quantised_model.inpaint = model.inpaint
547
+ quantised_model.ode_inpaint = model.ode_inpaint
548
+ return quantised_model
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: bayesianflow_for_chem
3
- Version: 1.2.1
3
+ Version: 1.2.2
4
4
  Summary: Bayesian flow network framework for Chemistry
5
5
  Home-page: https://augus1999.github.io/bayesian-flow-network-for-chemistry/
6
6
  Author: Nianze A. Tao