Skip to content

Commit c33fdd8

Browse files
committed
updata parameters in example
1 parent cf96029 commit c33fdd8

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

examples/example_1.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
os.environ["MASTER_PORT"] = os.getenv("MASTER_PORT", "12355")
2929

3030
backend = "nccl"
31+
# backend = "gloo"
3132

3233

3334
def init_process(rank, world_size, fn, backend=backend):
@@ -60,11 +61,18 @@ def half_sphere_integrand(x, f):
6061
bounds = [(-1, 1), (-1, 1)]
6162
n_eval = 6400000
6263
batch_size = 40000
64+
alpha = 2.0
65+
ninc = 1000
6366
n_therm = 20
6467

65-
device = torch.device(f"cuda:{rank}")
68+
if backend == "gloo":
69+
device = torch.device("cpu")
70+
elif backend == "nccl":
71+
device = torch.device(f"cuda:{rank}")
72+
else:
73+
raise ValueError(f"Invalid backend: {backend}")
6674

67-
vegas_map = Vegas(dim, device=device, ninc=10)
75+
vegas_map = Vegas(dim, device=device, ninc=ninc)
6876

6977
# Monte Carlo and MCMC for Unit Circle
7078
mc_integrator = MonteCarlo(
@@ -83,7 +91,7 @@ def half_sphere_integrand(x, f):
8391
print("MCMC:", mcmc_integrator(n_eval, mix_rate=0.5))
8492

8593
# Train VEGAS map for Unit Circle
86-
vegas_map.adaptive_training(batch_size, unit_circle_integrand, alpha=0.5)
94+
vegas_map.adaptive_training(batch_size, unit_circle_integrand, alpha=alpha)
8795
vegas_integrator = MonteCarlo(
8896
bounds,
8997
f=unit_circle_integrand,
@@ -113,7 +121,7 @@ def half_sphere_integrand(x, f):
113121

114122
vegas_map.make_uniform()
115123
vegas_map.adaptive_training(
116-
batch_size, half_sphere_integrand, epoch=10, alpha=0.5
124+
batch_size, half_sphere_integrand, epoch=10, alpha=alpha
117125
)
118126
vegas_integrator.f = half_sphere_integrand
119127
vegasmcmc_integrator.f = half_sphere_integrand

examples/example_2.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
os.environ["MASTER_PORT"] = os.getenv("MASTER_PORT", "12355")
2929

3030
backend = "nccl"
31+
# backend = "gloo"
3132

3233

3334
def init_process(rank, world_size, fn, backend=backend):
@@ -60,13 +61,20 @@ def sharp_integrands(x, f):
6061
bounds = [(0, 1)] * dim
6162
n_eval = 6400000
6263
batch_size = 40000
64+
alpha = 2.0
65+
ninc = 1000
6366
n_therm = 20
6467

65-
device = torch.device(f"cuda:{rank}")
68+
if backend == "gloo":
69+
device = torch.device("cpu")
70+
elif backend == "nccl":
71+
device = torch.device(f"cuda:{rank}")
72+
else:
73+
raise ValueError(f"Invalid backend: {backend}")
6674

6775
print(f"Process {rank} using device: {device}")
6876

69-
vegas_map = Vegas(dim, device=device, ninc=1000)
77+
vegas_map = Vegas(dim, device=device, ninc=ninc)
7078

7179
# Plain MC and MCMC
7280
mc_integrator = MonteCarlo(
@@ -91,7 +99,7 @@ def sharp_integrands(x, f):
9199

92100
# Train VEGAS map
93101
vegas_map.adaptive_training(
94-
batch_size, sharp_integrands, f_dim=3, epoch=10, alpha=2.0
102+
batch_size, sharp_integrands, f_dim=3, epoch=10, alpha=alpha
95103
)
96104
vegas_integrator = MonteCarlo(
97105
bounds,

examples/example_3.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import sys
66
import traceback
77
from MCintegration import MonteCarlo, MarkovChainMonteCarlo, Vegas
8+
89
os.environ["NCCL_DEBUG"] = "OFF"
910
os.environ["TORCH_DISTRIBUTED_DEBUG"] = "OFF"
1011
os.environ["GLOG_minloglevel"] = "2"
1112
os.environ["MASTER_ADDR"] = os.getenv("MASTER_ADDR", "localhost")
1213
os.environ["MASTER_PORT"] = os.getenv("MASTER_PORT", "12355")
1314

1415
backend = "nccl"
16+
# backend = "gloo"
17+
1518

1619
def init_process(rank, world_size, fn, backend=backend):
1720
try:
@@ -23,6 +26,7 @@ def init_process(rank, world_size, fn, backend=backend):
2326
dist.destroy_process_group()
2427
raise e
2528

29+
2630
def run_mcmc(rank, world_size):
2731
try:
2832
if rank != 0:
@@ -42,7 +46,12 @@ def func(x, f):
4246
ninc = 1000
4347
n_therm = 20
4448

45-
device = torch.device(f"cuda:{rank}")
49+
if backend == "gloo":
50+
device = torch.device("cpu")
51+
elif backend == "nccl":
52+
device = torch.device(f"cuda:{rank}")
53+
else:
54+
raise ValueError(f"Invalid backend: {backend}")
4655

4756
print(f"Process {rank} using device: {device}")
4857

@@ -54,26 +63,32 @@ def func(x, f):
5463

5564
print("Integration Results for log(x)/sqrt(x):")
5665

57-
5866
# Plain MC Integration
59-
mc_integrator = MonteCarlo(bounds, func, batch_size=batch_size,device=device)
67+
mc_integrator = MonteCarlo(bounds, func, batch_size=batch_size, device=device)
6068
print("Plain MC Integral Result:", mc_integrator(n_eval))
6169

6270
# MCMC Integration
6371
mcmc_integrator = MarkovChainMonteCarlo(
64-
bounds, func, batch_size=batch_size, nburnin=n_therm,device=device
72+
bounds, func, batch_size=batch_size, nburnin=n_therm, device=device
6573
)
6674
print("MCMC Integral Result:", mcmc_integrator(n_eval, mix_rate=0.5))
6775

6876
# Perform VEGAS integration
69-
vegas_integrator = MonteCarlo(bounds, func, maps=vegas_map, batch_size=batch_size,device=device)
77+
vegas_integrator = MonteCarlo(
78+
bounds, func, maps=vegas_map, batch_size=batch_size, device=device
79+
)
7080
res = vegas_integrator(n_eval)
7181

7282
print("VEGAS Integral Result:", res)
7383

7484
# VEGAS-MCMC Integration
7585
vegasmcmc_integrator = MarkovChainMonteCarlo(
76-
bounds, func, maps=vegas_map, batch_size=batch_size, nburnin=n_therm,device=device
86+
bounds,
87+
func,
88+
maps=vegas_map,
89+
batch_size=batch_size,
90+
nburnin=n_therm,
91+
device=device,
7792
)
7893
res_vegasmcmc = vegasmcmc_integrator(n_eval, mix_rate=0.5)
7994
print("VEGAS-MCMC Integral Result:", res_vegasmcmc)
@@ -100,6 +115,7 @@ def test_mcmc(world_size):
100115
except Exception as e:
101116
print(f"Error in test_mcmc: {e}")
102117

118+
103119
if __name__ == "__main__":
104120
mp.set_start_method("spawn", force=True)
105-
test_mcmc(4)
121+
test_mcmc(4)

0 commit comments

Comments
 (0)