lab-cosmo / mops

Mathematical operations with GPU acceleration
BSD 3-Clause "New" or "Revised" License
7 stars 1 forks source link

Mops

Mathematical operations to extract all the performance juice from your hardware!

Getting the code

git clone https://github.com/lab-cosmo/mops
cd mops

# Builds the code and run all tests
tox

# Installs the Python package
pip install .

Planned operations

Some common motifs of vector, matrix and tensor operations that appear in science and engineering are planned to be implemented here for CPUs and GPUs. These include:

1. Homogeneous Polynomial Evaluation

Mathematical notation

$$ Oi = \sum{j=1}^J Cj \prod{k=1}^K A{iP{jk}} $$

Inputs

Output

$O$ is a dense 1D array of floats, which only contains a batch dimension of size $I$.

Calculation

The calculation consists in a batched evaluation of homogeneous polynomials of degree $K$, where the monomials are given by $C[j] A[:, P_1[j, 1]] A[:, P_2[j, 2]] * \dots$, as follows:

for j in range(J):
    O[:] += C[j] * A[:, P_1[j, 1]] * A[:, P_2[j, 2]] * ...

2. Sparse Accumulation of Products

Mathematical notation

$$ O_{iPk^O} = \sum{k \in {k'|P^O_{k'}=P^O_k}} Ck A{iPk^A} B{iP_k^B} $$

Inputs

Output

$O$ is a 2D array of floats where the first dimension is a batch dimension (the same as in $A$ and $B$) and the second dimension contains the scattered products of $A$ and $B$.

Calculation

The weighted products of $A$ and $B$ are accumulated into $O$ as follows:

for k in range(K):
    O[:, P_O[k]] += C[k] * A[:, P_A[k]] * B[:, P_B[k]]

3. Outer Product Scatter-Add

Math notation

$$ O{ikl} = \sum{j=1}^J A{jk} B{jl} \delta_{iPj} \hspace{1cm} \mathrm{or} \hspace{1cm} O{ikl} = \sum{j \in {j'|P{j'}=i}} A{jk} B{jl} $$

Inputs

Output

$O$ is a 3D array of floats of dimensions $I \times K \times L$, which contains the accumulated products of the elements of $A$ and $B$.

Calculation

For each $j$, an outer product of $A[j, :]$ and $B[j, :]$ is calculated, and it is summed to $O[P[j], :, :]$:

for j in range(J):
    O[P[j], :, :] += A[j, :, None] * B[j, None, :]

4. Outer product Scatter-Add with Weights

Math notation

$$ O{ikl} = \sum{j \in {j'|P{j'}=i}} A{jk} B{jl} W{{PW_j}l} $$

Inputs

Outputs

Calculation

for j in range(J):
    O[PO[j], :, :] += A[j, :, None] * B[j, None, :] * W[PW[j], None, :]

5. Sparse Accumulation Scatter-Add with Weights

Math notation

$$ O_{i{m3}k} = \sum{e \in {e'|I{e'}=i}} R{ek} \sum{n \in {n'|M^3{n'}=m_3}} Cn A{e{Mn^1}} X{{J_e}{M_n^2}k} $$

Inputs

Inputs

Outputs

Outputs

Calculation

for j in range(J):
    for n in range(N):
        O[PO1[e], PO2[n], :] += A[e, PA[n]] * B[e, :] * C[n] * W[PW1[e], PW2[n], :]