Coder Social home page Coder Social logo

Comments (3)

gomezzz avatar gomezzz commented on August 25, 2024

@FHof I think you also did this, right?

from torchquad.

FHof avatar FHof commented on August 25, 2024

No, I haven't vectorized the VEGASMap over the dimension everywhere, but this vectorization could be possible.
I think the _add_at_indices function currently only works with 1D tensors, but it could be extended for multiple dimensions.
Here are the current for loops over the dimension:

for i in range(self.dim):
ID_i = ID[:, i]
res[:, i] = self.x_edges[i, ID_i] + self.dx_edges[i, ID_i] * offset[:, i]

for i in range(self.dim):
ID_i = ID[:, i]
jac *= self.N_intervals * self.dx_edges[i][ID_i]

for i in range(self.dim):
ID_i = ID[:, i]
_add_at_indices(self.weights[i], ID_i, jf_vec2)
_add_at_indices(self.counts[i], ID_i, ones)

for i in range(self.dim): # Update per dim
delta_d = delta_weights[i]
# For each inner edge, determine how many delta_d fit into the
# accumulated smoothed weights.
# With torch, CUDA and a high number of points the cumsum operation
# with float32 precision is too inaccurate which leads to wrong
# indices, so cast to float64 here.
delta_d_multiples = astype(
anp.cumsum(astype(smoothed_weights[i, :-1], "float64"), axis=0)
/ delta_d,
"int64",
)
# For each number of delta_d multiples in {0, 1, …, N_intervals},
# determine how many intervals belong to it (num_sw_per_dw)
# and the sum of smoothed weights in these intervals (val_sw_per_dw)
dtype_int = delta_d_multiples.dtype
num_sw_per_dw = anp.zeros(
[self.N_intervals + 1], dtype=dtype_int, like=delta_d
)
_add_at_indices(
num_sw_per_dw,
delta_d_multiples,
anp.ones(delta_d_multiples.shape, dtype=dtype_int, like=delta_d),
is_sorted=True,
)
val_sw_per_dw = anp.zeros(
[self.N_intervals + 1], dtype=self.dtype, like=delta_d
)
_add_at_indices(
val_sw_per_dw, delta_d_multiples, smoothed_weights[i], is_sorted=True
)
# The cumulative sum of the number of smoothed weights per delta_d
# multiple determines the old inner edges indices for the new inner
# edges calculation
indices = anp.cumsum(num_sw_per_dw[:-2], axis=0)
# d_accu_i is used for the interpolation in the new inner edges
# calculation when adding it to the old inner edges
d_accu_i = anp.cumsum(delta_d - val_sw_per_dw[:-2], axis=0)
# EQ 22
self.x_edges[i][1:-1] = (
self.x_edges[i][indices]
+ d_accu_i / smoothed_weights[i][indices] * self.dx_edges[i][indices]
)
finite_edges = anp.isfinite(self.x_edges[i])
if not anp.all(finite_edges):
# With float64 precision the delta_d_multiples calculation
# usually doesn't have rounding errors.
# If it is nonetheless too inaccurate, few values in
# smoothed_weights[i][indices] can be zero, which leads to
# invalid edges.
num_edges = self.x_edges.shape[1]
logger.warning(
f"{num_edges - anp.sum(finite_edges)} out of {num_edges} calculated VEGASMap edges were infinite"
)
# Replace inf edges with the average of their two neighbours
middle_edges = 0.5 * (self.x_edges[i][:-2] + self.x_edges[i][2:])
self.x_edges[i][1:-1] = anp.where(
finite_edges[1:-1], self.x_edges[i][1:-1], middle_edges
)
if not anp.all(anp.isfinite(self.x_edges[i])):
raise RuntimeError("Could not replace all infinite edges")
self.dx_edges[i] = self.x_edges[i][1:] - self.x_edges[i][:-1]

From the functions listed in the issue description, I have only vectorized VEGASStratification._get_indices and VEGASMap._smooth_map over the dimension.

I mainly vectorized the code with respect to values depending on the number of evaluations. With a four-dimensional integrand and a high number of points, this made VEGAS about 300 times faster: FHof#13 (comment)

from torchquad.

gomezzz avatar gomezzz commented on August 25, 2024

Ok, in any event, I think the performance should for now be quite a lot better. I will close it for now and we can reopen if we find it to be too slow at a later point. Thanks!

from torchquad.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.