Implement S2 R2-G24 and Transolver query models with synthetic acceptance
This commit is contained in:
1 parent
d4e26d48d0
commit
bb09e8e164
17 files changed
+1734
No files matched your search
@@ -0,0 +1 @@
|
||||
"""S2 synthetic-validated model and feature primitives; no data loaders or training entrypoint."""
|
||||
@@ -0,0 +1,59 @@
|
||||
# Adapted from Transolver 75e0f676; MIT, see upstream/LICENSE.
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
|
||||
|
||||
class Physics_Attention_Irregular_Mesh(nn.Module):
|
||||
## for irregular meshes in 1D, 2D or 3D space
|
||||
def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=64):
|
||||
super().__init__()
|
||||
inner_dim = dim_head * heads
|
||||
self.dim_head = dim_head
|
||||
self.heads = heads
|
||||
self.scale = dim_head ** -0.5
|
||||
self.softmax = nn.Softmax(dim=-1)
|
||||
self.dropout = nn.Dropout(dropout)
|
||||
self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
|
||||
self.in_project_x = nn.Linear(dim, inner_dim)
|
||||
self.in_project_fx = nn.Linear(dim, inner_dim)
|
||||
self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
for l in [self.in_project_slice]:
|
||||
torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_out = nn.Sequential(
|
||||
nn.Linear(inner_dim, dim),
|
||||
nn.Dropout(dropout)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
# B N C
|
||||
B, N, C = x.shape
|
||||
|
||||
### (1) Slice
|
||||
fx_mid = self.in_project_fx(x).reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
x_mid = self.in_project_x(x).reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
slice_weights = self.softmax(self.in_project_slice(x_mid) / self.temperature) # B H N G
|
||||
slice_norm = slice_weights.sum(2) # B H G
|
||||
slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
|
||||
### (2) Attention among slice tokens
|
||||
q_slice_token = self.to_q(slice_token)
|
||||
k_slice_token = self.to_k(slice_token)
|
||||
v_slice_token = self.to_v(slice_token)
|
||||
dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
attn = self.softmax(dots)
|
||||
attn = self.dropout(attn)
|
||||
out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
|
||||
### (3) Deslice
|
||||
out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
out_x = out_x.permute(0, 2, 1, 3).reshape(B, N, -1)
|
||||
return self.to_out(out_x)
|
||||
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# Adapted from Transolver 75e0f676; MIT, see upstream/LICENSE.
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from .attention import Physics_Attention_Irregular_Mesh
|
||||
|
||||
ACTIVATION = {'gelu': nn.GELU, 'tanh': nn.Tanh, 'sigmoid': nn.Sigmoid, 'relu': nn.ReLU, 'leaky_relu': nn.LeakyReLU(0.1),
|
||||
'softplus': nn.Softplus, 'ELU': nn.ELU, 'silu': nn.SiLU}
|
||||
|
||||
|
||||
class MLP(nn.Module):
|
||||
def __init__(self, n_input, n_hidden, n_output, n_layers=1, act='gelu', res=True):
|
||||
super(MLP, self).__init__()
|
||||
|
||||
if act in ACTIVATION.keys():
|
||||
act = ACTIVATION[act]
|
||||
else:
|
||||
raise NotImplementedError
|
||||
self.n_input = n_input
|
||||
self.n_hidden = n_hidden
|
||||
self.n_output = n_output
|
||||
self.n_layers = n_layers
|
||||
self.res = res
|
||||
self.linear_pre = nn.Sequential(nn.Linear(n_input, n_hidden), act())
|
||||
self.linear_post = nn.Linear(n_hidden, n_output)
|
||||
self.linears = nn.ModuleList([nn.Sequential(nn.Linear(n_hidden, n_hidden), act()) for _ in range(n_layers)])
|
||||
|
||||
def forward(self, x):
|
||||
x = self.linear_pre(x)
|
||||
for i in range(self.n_layers):
|
||||
if self.res:
|
||||
x = self.linears[i](x) + x
|
||||
else:
|
||||
x = self.linears[i](x)
|
||||
x = self.linear_post(x)
|
||||
return x
|
||||
|
||||
|
||||
class Transolver_block(nn.Module):
|
||||
"""Transformer encoder block."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
num_heads: int,
|
||||
hidden_dim: int,
|
||||
dropout: float,
|
||||
act='gelu',
|
||||
mlp_ratio=4,
|
||||
last_layer=False,
|
||||
out_dim=1,
|
||||
slice_num=32,
|
||||
):
|
||||
super().__init__()
|
||||
self.last_layer = last_layer
|
||||
self.ln_1 = nn.LayerNorm(hidden_dim)
|
||||
self.Attn = Physics_Attention_Irregular_Mesh(hidden_dim, heads=num_heads, dim_head=hidden_dim // num_heads,
|
||||
dropout=dropout, slice_num=slice_num)
|
||||
self.ln_2 = nn.LayerNorm(hidden_dim)
|
||||
self.mlp = MLP(hidden_dim, hidden_dim * mlp_ratio, hidden_dim, n_layers=0, res=False, act=act)
|
||||
if self.last_layer:
|
||||
self.ln_3 = nn.LayerNorm(hidden_dim)
|
||||
self.mlp2 = nn.Linear(hidden_dim, out_dim)
|
||||
|
||||
def forward(self, fx):
|
||||
fx = self.Attn(self.ln_1(fx)) + fx
|
||||
fx = self.mlp(self.ln_2(fx)) + fx
|
||||
if self.last_layer:
|
||||
return self.mlp2(self.ln_3(fx))
|
||||
else:
|
||||
return fx
|
||||
|
||||
|
||||
@@ -0,0 +1,169 @@
|
||||
"""Array-only R2-G24 graph approximation. No dataset access or learned state."""
|
||||
from itertools import combinations, product
|
||||
|
||||
import numpy as np
|
||||
from scipy.sparse import coo_matrix
|
||||
from scipy.sparse.csgraph import dijkstra
|
||||
from scipy.spatial import cKDTree
|
||||
|
||||
|
||||
def finite_array(value, shape=None, name="array"):
|
||||
a = np.asarray(value, dtype=np.float64)
|
||||
if not np.isfinite(a).all() or (shape is not None and a.shape != shape):
|
||||
raise ValueError(f"invalid {name}: expected finite {shape}, got {a.shape}")
|
||||
return a
|
||||
|
||||
|
||||
def mesh_geometry(points, tets):
|
||||
x = finite_array(points, name="points")
|
||||
t = np.asarray(tets)
|
||||
if x.ndim != 2 or x.shape[1] != 3 or not len(x):
|
||||
raise ValueError("points must be nonempty (N,3)")
|
||||
if t.ndim != 2 or t.shape[1] != 4 or not len(t) or t.dtype.kind not in "iu":
|
||||
raise ValueError("tets must be nonempty integer (M,4)")
|
||||
if t.min() < 0 or t.max() >= len(x):
|
||||
raise ValueError("tet node outside points")
|
||||
cells = x[t]
|
||||
matrices = cells[:, 1:] - cells[:, :1]
|
||||
volumes = np.abs(np.linalg.det(matrices)) / 6
|
||||
if not np.isfinite(volumes).all() or np.any(volumes <= 0):
|
||||
raise ValueError("degenerate tetrahedron")
|
||||
mass = np.bincount(t.ravel(), weights=np.repeat(volumes / 4, 4), minlength=len(x))
|
||||
if np.any(mass <= 0):
|
||||
raise ValueError("unused mesh node")
|
||||
edges = np.unique(np.sort(np.concatenate([t[:, [i, j]] for i, j in combinations(range(4), 2)]), axis=1), axis=0)
|
||||
lengths = np.linalg.norm(x[edges[:, 1]] - x[edges[:, 0]], axis=1)
|
||||
return x, t, matrices, volumes, mass, edges, lengths
|
||||
|
||||
|
||||
def graph_distances(points, tets, wall_distance, gate, *, exclude_wall_edges=True):
|
||||
x, t, matrices, vol, mass, edges, lengths = mesh_geometry(points, tets)
|
||||
dw = finite_array(wall_distance, (len(x),), "wall distance")
|
||||
gate = finite_array(gate, (3,), "gate")
|
||||
if np.any(dw < 0):
|
||||
raise ValueError("negative wall distance")
|
||||
# argmin returns the first node in original ascending node-ID order.
|
||||
source = int(np.argmin(np.linalg.norm(x - gate, axis=1)))
|
||||
keep = (dw[edges].sum(axis=1) > 0) if exclude_wall_edges else np.ones(len(edges), dtype=bool)
|
||||
e, length = edges[keep], lengths[keep]
|
||||
|
||||
def solve(cost):
|
||||
graph = coo_matrix((np.tile(cost, 2), (np.r_[e[:, 0], e[:, 1]], np.r_[e[:, 1], e[:, 0]])), shape=(len(x), len(x))).tocsr()
|
||||
out = dijkstra(graph, directed=False, indices=source)
|
||||
if not np.isfinite(out).all():
|
||||
raise ValueError("unreachable tet node after graph-edge rule")
|
||||
return out
|
||||
|
||||
di = solve(length)
|
||||
# Unrestricted geodesic is a separate N/E base input, not flow distance.
|
||||
if not exclude_wall_edges:
|
||||
return di, source
|
||||
df = solve(length / dw[e].mean(axis=1))
|
||||
return di, df, source
|
||||
|
||||
|
||||
def relative_flow_volume(flow_distance, lumped_mass):
|
||||
"""Strict front CDF: sum masses with D_f[j] < D_f[i]; ties share a value."""
|
||||
d = finite_array(flow_distance, name="flow distance")
|
||||
m = finite_array(lumped_mass, d.shape, "lumped mass")
|
||||
if d.ndim != 1 or not len(d) or np.any(m <= 0):
|
||||
raise ValueError("invalid volume CDF inputs")
|
||||
order = np.argsort(d, kind="stable")
|
||||
prefix = np.r_[0., np.cumsum(m[order])]
|
||||
return prefix[np.searchsorted(d[order], d, side="left")] / m.sum()
|
||||
|
||||
|
||||
def p1_nodal_gradient(points, tets, values, *, normalize=True):
|
||||
"""Volume-average constant cell P1 gradients, then unit-normalise at nodes.
|
||||
|
||||
An exactly zero averaged gradient stays zero; no epsilon direction is invented.
|
||||
"""
|
||||
x, t, matrices, volumes, mass, _, _ = mesh_geometry(points, tets)
|
||||
v = finite_array(values, (len(x),), "P1 nodal values")
|
||||
cell_grad = np.linalg.solve(matrices, (v[t[:, 1:]] - v[t[:, :1]])[..., None])[..., 0]
|
||||
grad = np.zeros((len(x), 3))
|
||||
for k in range(4):
|
||||
np.add.at(grad, t[:, k], cell_grad * volumes[:, None])
|
||||
grad /= (4 * mass[:, None])
|
||||
if normalize:
|
||||
norm = np.linalg.norm(grad, axis=1)
|
||||
np.divide(grad, norm[:, None], out=grad, where=norm[:, None] > 0)
|
||||
return grad
|
||||
|
||||
|
||||
def spatial_moment(points, tets, injection_distance):
|
||||
"""Volume-weighted unit-direction moment with two strict neighbourhood tests.
|
||||
|
||||
Claude 1794667074.550589: exclude coincident cell centres before normalising
|
||||
volumes. Trace is one. Empty neighbourhoods fail closed.
|
||||
"""
|
||||
x, t, _, volumes, _, _, lengths = mesh_geometry(points, tets)
|
||||
di = finite_array(injection_distance, (len(x),), "injection distance")
|
||||
radius = 3 * lengths.max()
|
||||
centers = x[t].mean(axis=1)
|
||||
cell_di = di[t].mean(axis=1)
|
||||
tree = cKDTree(centers)
|
||||
result = np.empty((len(x), 3, 3))
|
||||
# One query at a time avoids storing all node/cell neighbourhoods together.
|
||||
for i, point in enumerate(x):
|
||||
ids = np.asarray(tree.query_ball_point(point, radius), dtype=np.int64)
|
||||
distance = np.linalg.norm(centers[ids] - point, axis=1)
|
||||
ids = ids[(np.abs(cell_di[ids] - di[i]) < radius) & (distance < radius) & (distance > 0)]
|
||||
if not len(ids):
|
||||
raise ValueError(f"empty spatial-moment neighbourhood at node {i}")
|
||||
delta = centers[ids] - point
|
||||
delta /= np.linalg.norm(delta, axis=1)[:, None]
|
||||
weight = volumes[ids] / volumes[ids].sum()
|
||||
result[i] = np.einsum("j,ja,jb->ab", weight, delta, delta)
|
||||
return result, float(radius)
|
||||
|
||||
|
||||
R2_COLUMNS = ("Vf", "grad_Vf_x", "grad_Vf_y", "grad_Vf_z", "Di_star",
|
||||
"grad_Di_x", "grad_Di_y", "grad_Di_z", "Dw", "Dw_star",
|
||||
"grad_Dw_x", "grad_Dw_y", "grad_Dw_z", "Sxx", "Sxy", "Sxz", "Syy", "Syz", "Szz", "t_set")
|
||||
|
||||
|
||||
def r2_features(points, tets, wall_distance, gate, t_set):
|
||||
x, t, _, _, mass, _, _ = mesh_geometry(points, tets)
|
||||
dw = finite_array(wall_distance, (len(x),), "wall distance")
|
||||
ts = float(t_set)
|
||||
if not np.isfinite(ts) or ts <= 0:
|
||||
raise ValueError("missing/nonpositive t_set")
|
||||
di, df, source = graph_distances(x, t, dw, gate)
|
||||
if di.max() <= 0 or dw.max() <= 0:
|
||||
raise ValueError("zero normalisation scale")
|
||||
di_star, dw_star = di / di.max(), dw / dw.max()
|
||||
vf = relative_flow_volume(df, mass)
|
||||
moment, radius = spatial_moment(x, t, di)
|
||||
sym = moment[:, [0, 0, 0, 1, 1, 2], [0, 1, 2, 1, 2, 2]]
|
||||
features = np.column_stack([vf, p1_nodal_gradient(x, t, vf), di_star,
|
||||
p1_nodal_gradient(x, t, di_star), dw, dw_star,
|
||||
p1_nodal_gradient(x, t, dw_star), sym, np.full(len(x), ts)])
|
||||
finite_array(features, (len(x), len(R2_COLUMNS)), "R2 inputs")
|
||||
return {"features": features, "columns": R2_COLUMNS, "Di": di, "Df": df,
|
||||
"Vf": vf, "S": moment, "radius": radius, "gate_node": source}
|
||||
|
||||
|
||||
BASE_COLUMNS = ("x_star", "y_star", "z_star", "d_over_t", "gate_distance_over_Rg",
|
||||
"geodesic_over_Rg", "Rg", "volume", "t_part", "t_set")
|
||||
|
||||
|
||||
def base_features(points, wall_distance, geodesic_distance, gate, center, rg, volume, t_part, t_set):
|
||||
x = finite_array(points, name="points")
|
||||
if x.ndim != 2 or x.shape[1] != 3 or not len(x):
|
||||
raise ValueError("invalid node coordinates")
|
||||
dw = finite_array(wall_distance, (len(x),), "wall distance")
|
||||
dg = finite_array(geodesic_distance, (len(x),), "geodesic")
|
||||
gate, center = finite_array(gate, (3,), "gate"), finite_array(center, (3,), "center")
|
||||
scales = finite_array([rg, volume, t_part, t_set], (4,), "physical scales")
|
||||
if np.any(scales <= 0) or np.any(dw < 0) or np.any(dg < 0):
|
||||
raise ValueError("nonpositive scale or negative distance")
|
||||
return np.column_stack([(x-center)/rg, dw/t_part, np.linalg.norm(x-gate, axis=1)/rg,
|
||||
dg/rg, np.tile(scales, (len(x), 1))])
|
||||
|
||||
|
||||
def histgbrt_grid():
|
||||
"""Twelve unfitted estimators' configurations; never launches fits."""
|
||||
return [dict(learning_rate=lr, max_leaf_nodes=leaf, max_iter=n, min_samples_leaf=20,
|
||||
random_state=0, early_stopping=False)
|
||||
for lr, leaf, n in product((.05, .1), (31, 63), (200, 500, 1000))]
|
||||
@@ -0,0 +1,109 @@
|
||||
"""Transolver 75e0f676 encoder adaptation + S2 query-only N/E decoders.
|
||||
|
||||
No optimiser, training loop, data reader or GPU invocation is provided.
|
||||
"""
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from .blocks import MLP, Transolver_block
|
||||
from .features import BASE_COLUMNS
|
||||
|
||||
|
||||
TRAINING_CONTRACT = dict(optimizer="AdamW", learning_rate=1e-3, weight_decay=1e-5,
|
||||
schedule="cosine", batch_parts=1, max_epochs=200,
|
||||
patience=20, seeds=[0, 1, 2], widths=[128, 256])
|
||||
|
||||
|
||||
def check_tensor(tensor, shape, name):
|
||||
if tuple(tensor.shape) != tuple(shape) or not torch.isfinite(tensor).all():
|
||||
raise ValueError(f"invalid {name}")
|
||||
|
||||
|
||||
class WholePartEncoder(nn.Module):
|
||||
def __init__(self, width):
|
||||
super().__init__()
|
||||
if width not in (128, 256):
|
||||
raise ValueError("S2 permits widths 128 or 256 only")
|
||||
self.preprocess = MLP(len(BASE_COLUMNS), width*2, width, n_layers=0, res=False)
|
||||
self.blocks = nn.ModuleList([Transolver_block(num_heads=8, hidden_dim=width,
|
||||
dropout=0., mlp_ratio=1, slice_num=32,
|
||||
last_layer=False) for _ in range(4)])
|
||||
self.apply(self._initialize)
|
||||
self.placeholder = nn.Parameter(torch.rand(width)/width)
|
||||
|
||||
@staticmethod
|
||||
def _initialize(module):
|
||||
# torch.nn.init replaces timm's same trunc_normal_ defaults; no timm dependency.
|
||||
if isinstance(module, nn.Linear):
|
||||
nn.init.trunc_normal_(module.weight, std=.02)
|
||||
if module.bias is not None:
|
||||
nn.init.zeros_(module.bias)
|
||||
elif isinstance(module, nn.LayerNorm):
|
||||
nn.init.zeros_(module.bias)
|
||||
nn.init.ones_(module.weight)
|
||||
|
||||
def forward(self, base):
|
||||
if base.ndim != 2 or base.shape[0] == 0:
|
||||
raise ValueError("one whole part with all tet nodes required")
|
||||
check_tensor(base, (base.shape[0], len(BASE_COLUMNS)), "base inputs")
|
||||
fx = self.preprocess(base[None]) + self.placeholder[None, None]
|
||||
for block in self.blocks:
|
||||
fx = block(fx)
|
||||
if not torch.isfinite(fx).all():
|
||||
raise ValueError("nonfinite encoder output")
|
||||
return fx[0]
|
||||
|
||||
|
||||
class QueryModel(nn.Module):
|
||||
def __init__(self, arm, width=128):
|
||||
super().__init__()
|
||||
if arm not in ("N", "E"):
|
||||
raise ValueError("arm must be N or E")
|
||||
self.arm, self.width = arm, width
|
||||
self.encoder = WholePartEncoder(width)
|
||||
query_dim = 2 if arm == "N" else 6
|
||||
self.decoder = nn.Sequential(nn.Linear(width+query_dim, width), nn.GELU(),
|
||||
nn.Linear(width, width), nn.GELU(), nn.Linear(width, 1))
|
||||
self.decoder.apply(WholePartEncoder._initialize)
|
||||
|
||||
def forward(self, base, query_ids, gaps, edge_q=None):
|
||||
if query_ids.ndim != 1 or query_ids.dtype != torch.long or query_ids.numel() == 0:
|
||||
raise ValueError("nonempty int64 original-node query IDs required")
|
||||
if int(query_ids.min()) < 0 or int(query_ids.max()) >= len(base):
|
||||
raise ValueError("query node outside whole part")
|
||||
check_tensor(gaps, (len(query_ids), 2), "g12/g23")
|
||||
if self.arm == "E":
|
||||
if edge_q is None:
|
||||
raise ValueError("E requires all four edge summaries")
|
||||
check_tensor(edge_q, (len(query_ids), 4), "edge summaries")
|
||||
local = torch.cat([gaps, edge_q], dim=-1)
|
||||
else:
|
||||
if edge_q is not None:
|
||||
raise ValueError("N must not receive edge summaries")
|
||||
local = gaps
|
||||
embedding = self.encoder(base)[query_ids]
|
||||
y = self.decoder(torch.cat([embedding, local], dim=-1)).squeeze(-1)
|
||||
if not torch.isfinite(y).all():
|
||||
raise ValueError("nonfinite decoder output")
|
||||
return y
|
||||
|
||||
def parameter_counts(self):
|
||||
return {"encoder": sum(p.numel() for p in self.encoder.parameters()),
|
||||
"decoder": sum(p.numel() for p in self.decoder.parameters()),
|
||||
"total": sum(p.numel() for p in self.parameters())}
|
||||
|
||||
|
||||
def weighted_mse(prediction, target, global_weights):
|
||||
"""Sum of contributions with already globally-normalised hierarchy weights.
|
||||
|
||||
Do not renormalise a single part's weights: that would erase component balance.
|
||||
"""
|
||||
if prediction.ndim != 1 or prediction.numel() == 0:
|
||||
raise ValueError("nonempty query predictions required")
|
||||
check_tensor(prediction, prediction.shape, "prediction")
|
||||
check_tensor(target, prediction.shape, "target")
|
||||
check_tensor(global_weights, prediction.shape, "weights")
|
||||
if (global_weights <= 0).any():
|
||||
raise ValueError("positive hierarchy weights required")
|
||||
# FP32 arithmetic under future AMP; do not form residuals in float16.
|
||||
return torch.sum(global_weights.float() * (prediction.float()-target.float()).square())
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Process-conditioned target, hierarchy weights and scoring; no fitting I/O."""
|
||||
import numpy as np
|
||||
|
||||
from .features import finite_array
|
||||
|
||||
|
||||
def positive_scale(value, name):
|
||||
v = float(value)
|
||||
if not np.isfinite(v) or v <= 0:
|
||||
raise ValueError(f"{name} must be finite and positive")
|
||||
return v
|
||||
|
||||
|
||||
def dimensionless_target(seconds, t_set):
|
||||
t = finite_array(seconds, name="seconds")
|
||||
if np.any(t < 0):
|
||||
raise ValueError("negative arrival time")
|
||||
return t / positive_scale(t_set, "t_set")
|
||||
|
||||
|
||||
def prediction_seconds(t_star, t_set):
|
||||
"""Direct target per Claude 1794667074.550589: no log transform or tau.
|
||||
|
||||
Predictions are not clipped; invalid output fails closed and is reported.
|
||||
"""
|
||||
t = finite_array(t_star, name="prediction t_star")
|
||||
return finite_array(t * positive_scale(t_set, "t_set"), name="predicted seconds")
|
||||
|
||||
|
||||
def b0_seconds(relative_volume, t_set):
|
||||
vf = finite_array(relative_volume, name="relative flow volume")
|
||||
if np.any((vf < 0) | (vf > 1)):
|
||||
raise ValueError("relative volume outside [0,1]")
|
||||
return vf * positive_scale(t_set, "t_set")
|
||||
|
||||
|
||||
def hierarchy_weights(draw_counts, part_ids, component_ids):
|
||||
"""Sum=1; equal components, equal parts in each, count-weighted nodes.
|
||||
|
||||
Call on the full learning pool, not on one minibatch. A batch-1-part
|
||||
objective sums each part's weighted contribution over the epoch; if
|
||||
sampling parts uniformly with replacement multiply each contribution by
|
||||
the number of learning parts for an unbiased per-step objective.
|
||||
"""
|
||||
c = finite_array(draw_counts, name="draw counts")
|
||||
p, g = np.asarray(part_ids), np.asarray(component_ids)
|
||||
if c.ndim != 1 or not len(c) or p.shape != c.shape or g.shape != c.shape:
|
||||
raise ValueError("invalid hierarchy shape")
|
||||
if np.any(c <= 0) or np.any(c != np.floor(c)):
|
||||
raise ValueError("only eligible positive draw counts accepted")
|
||||
groups = np.unique(g)
|
||||
w = np.empty_like(c)
|
||||
for part in np.unique(p):
|
||||
mask = p == part
|
||||
if len(np.unique(g[mask])) != 1:
|
||||
raise ValueError("one part belongs to multiple components")
|
||||
for group in groups:
|
||||
mask_g = g == group
|
||||
parts = np.unique(p[mask_g])
|
||||
for part in parts:
|
||||
mask = mask_g & (p == part)
|
||||
w[mask] = c[mask] / c[mask].sum() / len(parts) / len(groups)
|
||||
return w
|
||||
|
||||
|
||||
def relative_l2(prediction, truth, draw_counts, eligible):
|
||||
p, t, c = np.asarray(prediction), np.asarray(truth), np.asarray(draw_counts)
|
||||
e = np.asarray(eligible)
|
||||
if p.ndim != 1 or t.shape != p.shape or c.shape != p.shape or e.shape != p.shape or e.dtype.kind != "b":
|
||||
raise ValueError("invalid score arrays")
|
||||
# Mask before any arithmetic: ineligible NaNs must never enter 0*NaN.
|
||||
p, t, c = (finite_array(v[e], name="eligible score input") for v in (p, t, c))
|
||||
if not len(c) or np.any(c <= 0) or np.any(c != np.floor(c)):
|
||||
raise ValueError("no eligible positive integer draw counts")
|
||||
denominator = np.sum(c*t*t)
|
||||
if denominator <= 0:
|
||||
return {"value": None, "reason": "zero truth norm"}
|
||||
value = float(np.sqrt(np.sum(c*(p-t)**2)/denominator))
|
||||
if not np.isfinite(value):
|
||||
raise ValueError("nonfinite relative L2")
|
||||
return {"value": value, "reason": None}
|
||||
@@ -0,0 +1,5 @@
|
||||
# Tested existing CPU execution environment. No installation was performed.
|
||||
numpy==1.26.4
|
||||
scipy==1.15.3
|
||||
scikit-learn==1.7.2
|
||||
torch==2.5.1+cu124
|
||||
@@ -0,0 +1,350 @@
|
||||
--- upstream/Physics_Attention.py
|
||||
+++ attention.py
|
||||
@@ -1,6 +1,6 @@
|
||||
+# Adapted from Transolver 75e0f676; MIT, see upstream/LICENSE.
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
-from einops import rearrange, repeat
|
||||
|
||||
|
||||
class Physics_Attention_Irregular_Mesh(nn.Module):
|
||||
@@ -53,123 +53,7 @@
|
||||
|
||||
### (3) Deslice
|
||||
out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
- out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
+ out_x = out_x.permute(0, 2, 1, 3).reshape(B, N, -1)
|
||||
return self.to_out(out_x)
|
||||
|
||||
|
||||
-class Physics_Attention_Structured_Mesh_2D(nn.Module):
|
||||
- ## for structured mesh in 2D space
|
||||
- def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=64, H=101, W=31, kernel=3): # kernel=3):
|
||||
- super().__init__()
|
||||
- inner_dim = dim_head * heads
|
||||
- self.dim_head = dim_head
|
||||
- self.heads = heads
|
||||
- self.scale = dim_head ** -0.5
|
||||
- self.softmax = nn.Softmax(dim=-1)
|
||||
- self.dropout = nn.Dropout(dropout)
|
||||
- self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
- self.H = H
|
||||
- self.W = W
|
||||
-
|
||||
- self.in_project_x = nn.Conv2d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
- self.in_project_fx = nn.Conv2d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
- self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
- for l in [self.in_project_slice]:
|
||||
- torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
- self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
- self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
- self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
-
|
||||
- self.to_out = nn.Sequential(
|
||||
- nn.Linear(inner_dim, dim),
|
||||
- nn.Dropout(dropout)
|
||||
- )
|
||||
-
|
||||
- def forward(self, x):
|
||||
- # B N C
|
||||
- B, N, C = x.shape
|
||||
- x = x.reshape(B, self.H, self.W, C).contiguous().permute(0, 3, 1, 2).contiguous() # B C H W
|
||||
-
|
||||
- ### (1) Slice
|
||||
- fx_mid = self.in_project_fx(x).permute(0, 2, 3, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
- .permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
- x_mid = self.in_project_x(x).permute(0, 2, 3, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
- .permute(0, 2, 1, 3).contiguous() # B H N G
|
||||
- slice_weights = self.softmax(
|
||||
- self.in_project_slice(x_mid) / torch.clamp(self.temperature, min=0.1, max=5)) # B H N G
|
||||
- slice_norm = slice_weights.sum(2) # B H G
|
||||
- slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
- slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
-
|
||||
- ### (2) Attention among slice tokens
|
||||
- q_slice_token = self.to_q(slice_token)
|
||||
- k_slice_token = self.to_k(slice_token)
|
||||
- v_slice_token = self.to_v(slice_token)
|
||||
- dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
- attn = self.softmax(dots)
|
||||
- attn = self.dropout(attn)
|
||||
- out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
-
|
||||
- ### (3) Deslice
|
||||
- out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
- out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
- return self.to_out(out_x)
|
||||
-
|
||||
-
|
||||
-class Physics_Attention_Structured_Mesh_3D(nn.Module):
|
||||
- ## for structured mesh in 3D space
|
||||
- def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=32, H=32, W=32, D=32, kernel=3):
|
||||
- super().__init__()
|
||||
- inner_dim = dim_head * heads
|
||||
- self.dim_head = dim_head
|
||||
- self.heads = heads
|
||||
- self.scale = dim_head ** -0.5
|
||||
- self.softmax = nn.Softmax(dim=-1)
|
||||
- self.dropout = nn.Dropout(dropout)
|
||||
- self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
- self.H = H
|
||||
- self.W = W
|
||||
- self.D = D
|
||||
-
|
||||
- self.in_project_x = nn.Conv3d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
- self.in_project_fx = nn.Conv3d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
- self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
- for l in [self.in_project_slice]:
|
||||
- torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
- self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
- self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
- self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
- self.to_out = nn.Sequential(
|
||||
- nn.Linear(inner_dim, dim),
|
||||
- nn.Dropout(dropout)
|
||||
- )
|
||||
-
|
||||
- def forward(self, x):
|
||||
- # B N C
|
||||
- B, N, C = x.shape
|
||||
- x = x.reshape(B, self.H, self.W, self.D, C).contiguous().permute(0, 4, 1, 2, 3).contiguous() # B C H W
|
||||
-
|
||||
- ### (1) Slice
|
||||
- fx_mid = self.in_project_fx(x).permute(0, 2, 3, 4, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
- .permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
- x_mid = self.in_project_x(x).permute(0, 2, 3, 4, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
- .permute(0, 2, 1, 3).contiguous() # B H N G
|
||||
- slice_weights = self.softmax(
|
||||
- self.in_project_slice(x_mid) / torch.clamp(self.temperature, min=0.1, max=5)) # B H N G
|
||||
- slice_norm = slice_weights.sum(2) # B H G
|
||||
- slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
- slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
-
|
||||
- ### (2) Attention among slice tokens
|
||||
- q_slice_token = self.to_q(slice_token)
|
||||
- k_slice_token = self.to_k(slice_token)
|
||||
- v_slice_token = self.to_v(slice_token)
|
||||
- dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
- attn = self.softmax(dots)
|
||||
- attn = self.dropout(attn)
|
||||
- out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
-
|
||||
- ### (3) Deslice
|
||||
- out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
- out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
- return self.to_out(out_x)
|
||||
--- upstream/Transolver_Irregular_Mesh.py
|
||||
+++ blocks.py
|
||||
@@ -1,9 +1,7 @@
|
||||
+# Adapted from Transolver 75e0f676; MIT, see upstream/LICENSE.
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
-from timm.models.layers import trunc_normal_
|
||||
-from model.Embedding import timestep_embedding
|
||||
-import numpy as np
|
||||
-from model.Physics_Attention import Physics_Attention_Irregular_Mesh
|
||||
+from .attention import Physics_Attention_Irregular_Mesh
|
||||
|
||||
ACTIVATION = {'gelu': nn.GELU, 'tanh': nn.Tanh, 'sigmoid': nn.Sigmoid, 'relu': nn.ReLU, 'leaky_relu': nn.LeakyReLU(0.1),
|
||||
'softplus': nn.Softplus, 'ELU': nn.ELU, 'silu': nn.SiLU}
|
||||
@@ -71,88 +69,3 @@
|
||||
return fx
|
||||
|
||||
|
||||
-class Model(nn.Module):
|
||||
- def __init__(self,
|
||||
- space_dim=1,
|
||||
- n_layers=5,
|
||||
- n_hidden=256,
|
||||
- dropout=0.0,
|
||||
- n_head=8,
|
||||
- Time_Input=False,
|
||||
- act='gelu',
|
||||
- mlp_ratio=1,
|
||||
- fun_dim=1,
|
||||
- out_dim=1,
|
||||
- slice_num=32,
|
||||
- ref=8,
|
||||
- unified_pos=False
|
||||
- ):
|
||||
- super(Model, self).__init__()
|
||||
- self.__name__ = 'Transolver_1D'
|
||||
- self.ref = ref
|
||||
- self.unified_pos = unified_pos
|
||||
- self.Time_Input = Time_Input
|
||||
- self.n_hidden = n_hidden
|
||||
- self.space_dim = space_dim
|
||||
- if self.unified_pos:
|
||||
- self.preprocess = MLP(fun_dim + self.ref * self.ref, n_hidden * 2, n_hidden, n_layers=0, res=False, act=act)
|
||||
- else:
|
||||
- self.preprocess = MLP(fun_dim + space_dim, n_hidden * 2, n_hidden, n_layers=0, res=False, act=act)
|
||||
- if Time_Input:
|
||||
- self.time_fc = nn.Sequential(nn.Linear(n_hidden, n_hidden), nn.SiLU(), nn.Linear(n_hidden, n_hidden))
|
||||
-
|
||||
- self.blocks = nn.ModuleList([Transolver_block(num_heads=n_head, hidden_dim=n_hidden,
|
||||
- dropout=dropout,
|
||||
- act=act,
|
||||
- mlp_ratio=mlp_ratio,
|
||||
- out_dim=out_dim,
|
||||
- slice_num=slice_num,
|
||||
- last_layer=(_ == n_layers - 1))
|
||||
- for _ in range(n_layers)])
|
||||
- self.initialize_weights()
|
||||
- self.placeholder = nn.Parameter((1 / (n_hidden)) * torch.rand(n_hidden, dtype=torch.float))
|
||||
-
|
||||
- def initialize_weights(self):
|
||||
- self.apply(self._init_weights)
|
||||
-
|
||||
- def _init_weights(self, m):
|
||||
- if isinstance(m, nn.Linear):
|
||||
- trunc_normal_(m.weight, std=0.02)
|
||||
- if isinstance(m, nn.Linear) and m.bias is not None:
|
||||
- nn.init.constant_(m.bias, 0)
|
||||
- elif isinstance(m, (nn.LayerNorm, nn.BatchNorm1d)):
|
||||
- nn.init.constant_(m.bias, 0)
|
||||
- nn.init.constant_(m.weight, 1.0)
|
||||
-
|
||||
- def get_grid(self, x, batchsize=1):
|
||||
- # x: B N 2
|
||||
- # grid_ref
|
||||
- gridx = torch.tensor(np.linspace(0, 1, self.ref), dtype=torch.float)
|
||||
- gridx = gridx.reshape(1, self.ref, 1, 1).repeat([batchsize, 1, self.ref, 1])
|
||||
- gridy = torch.tensor(np.linspace(0, 1, self.ref), dtype=torch.float)
|
||||
- gridy = gridy.reshape(1, 1, self.ref, 1).repeat([batchsize, self.ref, 1, 1])
|
||||
- grid_ref = torch.cat((gridx, gridy), dim=-1).cuda().reshape(batchsize, self.ref * self.ref, 2) # B H W 8 8 2
|
||||
-
|
||||
- pos = torch.sqrt(torch.sum((x[:, :, None, :] - grid_ref[:, None, :, :]) ** 2, dim=-1)). \
|
||||
- reshape(batchsize, x.shape[1], self.ref * self.ref).contiguous()
|
||||
- return pos
|
||||
-
|
||||
- def forward(self, x, fx, T=None):
|
||||
- if self.unified_pos:
|
||||
- x = self.get_grid(x, x.shape[0])
|
||||
- if fx is not None:
|
||||
- fx = torch.cat((x, fx), -1)
|
||||
- fx = self.preprocess(fx)
|
||||
- else:
|
||||
- fx = self.preprocess(x)
|
||||
- fx = fx + self.placeholder[None, None, :]
|
||||
-
|
||||
- if T is not None:
|
||||
- Time_emb = timestep_embedding(T, self.n_hidden).repeat(1, x.shape[1], 1)
|
||||
- Time_emb = self.time_fc(Time_emb)
|
||||
- fx = fx + Time_emb
|
||||
-
|
||||
- for block in self.blocks:
|
||||
- fx = block(fx)
|
||||
-
|
||||
- return fx
|
||||
--- /dev/null
|
||||
+++ network.py
|
||||
@@ -0,0 +1,109 @@
|
||||
+"""Transolver 75e0f676 encoder adaptation + S2 query-only N/E decoders.
|
||||
+
|
||||
+No optimiser, training loop, data reader or GPU invocation is provided.
|
||||
+"""
|
||||
+import torch
|
||||
+from torch import nn
|
||||
+
|
||||
+from .blocks import MLP, Transolver_block
|
||||
+from .features import BASE_COLUMNS
|
||||
+
|
||||
+
|
||||
+TRAINING_CONTRACT = dict(optimizer="AdamW", learning_rate=1e-3, weight_decay=1e-5,
|
||||
+ schedule="cosine", batch_parts=1, max_epochs=200,
|
||||
+ patience=20, seeds=[0, 1, 2], widths=[128, 256])
|
||||
+
|
||||
+
|
||||
+def check_tensor(tensor, shape, name):
|
||||
+ if tuple(tensor.shape) != tuple(shape) or not torch.isfinite(tensor).all():
|
||||
+ raise ValueError(f"invalid {name}")
|
||||
+
|
||||
+
|
||||
+class WholePartEncoder(nn.Module):
|
||||
+ def __init__(self, width):
|
||||
+ super().__init__()
|
||||
+ if width not in (128, 256):
|
||||
+ raise ValueError("S2 permits widths 128 or 256 only")
|
||||
+ self.preprocess = MLP(len(BASE_COLUMNS), width*2, width, n_layers=0, res=False)
|
||||
+ self.blocks = nn.ModuleList([Transolver_block(num_heads=8, hidden_dim=width,
|
||||
+ dropout=0., mlp_ratio=1, slice_num=32,
|
||||
+ last_layer=False) for _ in range(4)])
|
||||
+ self.apply(self._initialize)
|
||||
+ self.placeholder = nn.Parameter(torch.rand(width)/width)
|
||||
+
|
||||
+ @staticmethod
|
||||
+ def _initialize(module):
|
||||
+ # torch.nn.init replaces timm's same trunc_normal_ defaults; no timm dependency.
|
||||
+ if isinstance(module, nn.Linear):
|
||||
+ nn.init.trunc_normal_(module.weight, std=.02)
|
||||
+ if module.bias is not None:
|
||||
+ nn.init.zeros_(module.bias)
|
||||
+ elif isinstance(module, nn.LayerNorm):
|
||||
+ nn.init.zeros_(module.bias)
|
||||
+ nn.init.ones_(module.weight)
|
||||
+
|
||||
+ def forward(self, base):
|
||||
+ if base.ndim != 2 or base.shape[0] == 0:
|
||||
+ raise ValueError("one whole part with all tet nodes required")
|
||||
+ check_tensor(base, (base.shape[0], len(BASE_COLUMNS)), "base inputs")
|
||||
+ fx = self.preprocess(base[None]) + self.placeholder[None, None]
|
||||
+ for block in self.blocks:
|
||||
+ fx = block(fx)
|
||||
+ if not torch.isfinite(fx).all():
|
||||
+ raise ValueError("nonfinite encoder output")
|
||||
+ return fx[0]
|
||||
+
|
||||
+
|
||||
+class QueryModel(nn.Module):
|
||||
+ def __init__(self, arm, width=128):
|
||||
+ super().__init__()
|
||||
+ if arm not in ("N", "E"):
|
||||
+ raise ValueError("arm must be N or E")
|
||||
+ self.arm, self.width = arm, width
|
||||
+ self.encoder = WholePartEncoder(width)
|
||||
+ query_dim = 2 if arm == "N" else 6
|
||||
+ self.decoder = nn.Sequential(nn.Linear(width+query_dim, width), nn.GELU(),
|
||||
+ nn.Linear(width, width), nn.GELU(), nn.Linear(width, 1))
|
||||
+ self.decoder.apply(WholePartEncoder._initialize)
|
||||
+
|
||||
+ def forward(self, base, query_ids, gaps, edge_q=None):
|
||||
+ if query_ids.ndim != 1 or query_ids.dtype != torch.long or query_ids.numel() == 0:
|
||||
+ raise ValueError("nonempty int64 original-node query IDs required")
|
||||
+ if int(query_ids.min()) < 0 or int(query_ids.max()) >= len(base):
|
||||
+ raise ValueError("query node outside whole part")
|
||||
+ check_tensor(gaps, (len(query_ids), 2), "g12/g23")
|
||||
+ if self.arm == "E":
|
||||
+ if edge_q is None:
|
||||
+ raise ValueError("E requires all four edge summaries")
|
||||
+ check_tensor(edge_q, (len(query_ids), 4), "edge summaries")
|
||||
+ local = torch.cat([gaps, edge_q], dim=-1)
|
||||
+ else:
|
||||
+ if edge_q is not None:
|
||||
+ raise ValueError("N must not receive edge summaries")
|
||||
+ local = gaps
|
||||
+ embedding = self.encoder(base)[query_ids]
|
||||
+ y = self.decoder(torch.cat([embedding, local], dim=-1)).squeeze(-1)
|
||||
+ if not torch.isfinite(y).all():
|
||||
+ raise ValueError("nonfinite decoder output")
|
||||
+ return y
|
||||
+
|
||||
+ def parameter_counts(self):
|
||||
+ return {"encoder": sum(p.numel() for p in self.encoder.parameters()),
|
||||
+ "decoder": sum(p.numel() for p in self.decoder.parameters()),
|
||||
+ "total": sum(p.numel() for p in self.parameters())}
|
||||
+
|
||||
+
|
||||
+def weighted_mse(prediction, target, global_weights):
|
||||
+ """Sum of contributions with already globally-normalised hierarchy weights.
|
||||
+
|
||||
+ Do not renormalise a single part's weights: that would erase component balance.
|
||||
+ """
|
||||
+ if prediction.ndim != 1 or prediction.numel() == 0:
|
||||
+ raise ValueError("nonempty query predictions required")
|
||||
+ check_tensor(prediction, prediction.shape, "prediction")
|
||||
+ check_tensor(target, prediction.shape, "target")
|
||||
+ check_tensor(global_weights, prediction.shape, "weights")
|
||||
+ if (global_weights <= 0).any():
|
||||
+ raise ValueError("positive hierarchy weights required")
|
||||
+ # FP32 arithmetic under future AMP; do not form residuals in float16.
|
||||
+ return torch.sum(global_weights.float() * (prediction.float()-target.float()).square())
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 THUML @ Tsinghua University
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,175 @@
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
from einops import rearrange, repeat
|
||||
|
||||
|
||||
class Physics_Attention_Irregular_Mesh(nn.Module):
|
||||
## for irregular meshes in 1D, 2D or 3D space
|
||||
def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=64):
|
||||
super().__init__()
|
||||
inner_dim = dim_head * heads
|
||||
self.dim_head = dim_head
|
||||
self.heads = heads
|
||||
self.scale = dim_head ** -0.5
|
||||
self.softmax = nn.Softmax(dim=-1)
|
||||
self.dropout = nn.Dropout(dropout)
|
||||
self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
|
||||
self.in_project_x = nn.Linear(dim, inner_dim)
|
||||
self.in_project_fx = nn.Linear(dim, inner_dim)
|
||||
self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
for l in [self.in_project_slice]:
|
||||
torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_out = nn.Sequential(
|
||||
nn.Linear(inner_dim, dim),
|
||||
nn.Dropout(dropout)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
# B N C
|
||||
B, N, C = x.shape
|
||||
|
||||
### (1) Slice
|
||||
fx_mid = self.in_project_fx(x).reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
x_mid = self.in_project_x(x).reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
slice_weights = self.softmax(self.in_project_slice(x_mid) / self.temperature) # B H N G
|
||||
slice_norm = slice_weights.sum(2) # B H G
|
||||
slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
|
||||
### (2) Attention among slice tokens
|
||||
q_slice_token = self.to_q(slice_token)
|
||||
k_slice_token = self.to_k(slice_token)
|
||||
v_slice_token = self.to_v(slice_token)
|
||||
dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
attn = self.softmax(dots)
|
||||
attn = self.dropout(attn)
|
||||
out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
|
||||
### (3) Deslice
|
||||
out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
return self.to_out(out_x)
|
||||
|
||||
|
||||
class Physics_Attention_Structured_Mesh_2D(nn.Module):
|
||||
## for structured mesh in 2D space
|
||||
def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=64, H=101, W=31, kernel=3): # kernel=3):
|
||||
super().__init__()
|
||||
inner_dim = dim_head * heads
|
||||
self.dim_head = dim_head
|
||||
self.heads = heads
|
||||
self.scale = dim_head ** -0.5
|
||||
self.softmax = nn.Softmax(dim=-1)
|
||||
self.dropout = nn.Dropout(dropout)
|
||||
self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
self.H = H
|
||||
self.W = W
|
||||
|
||||
self.in_project_x = nn.Conv2d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
self.in_project_fx = nn.Conv2d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
for l in [self.in_project_slice]:
|
||||
torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
|
||||
self.to_out = nn.Sequential(
|
||||
nn.Linear(inner_dim, dim),
|
||||
nn.Dropout(dropout)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
# B N C
|
||||
B, N, C = x.shape
|
||||
x = x.reshape(B, self.H, self.W, C).contiguous().permute(0, 3, 1, 2).contiguous() # B C H W
|
||||
|
||||
### (1) Slice
|
||||
fx_mid = self.in_project_fx(x).permute(0, 2, 3, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
x_mid = self.in_project_x(x).permute(0, 2, 3, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N G
|
||||
slice_weights = self.softmax(
|
||||
self.in_project_slice(x_mid) / torch.clamp(self.temperature, min=0.1, max=5)) # B H N G
|
||||
slice_norm = slice_weights.sum(2) # B H G
|
||||
slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
|
||||
### (2) Attention among slice tokens
|
||||
q_slice_token = self.to_q(slice_token)
|
||||
k_slice_token = self.to_k(slice_token)
|
||||
v_slice_token = self.to_v(slice_token)
|
||||
dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
attn = self.softmax(dots)
|
||||
attn = self.dropout(attn)
|
||||
out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
|
||||
### (3) Deslice
|
||||
out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
return self.to_out(out_x)
|
||||
|
||||
|
||||
class Physics_Attention_Structured_Mesh_3D(nn.Module):
|
||||
## for structured mesh in 3D space
|
||||
def __init__(self, dim, heads=8, dim_head=64, dropout=0., slice_num=32, H=32, W=32, D=32, kernel=3):
|
||||
super().__init__()
|
||||
inner_dim = dim_head * heads
|
||||
self.dim_head = dim_head
|
||||
self.heads = heads
|
||||
self.scale = dim_head ** -0.5
|
||||
self.softmax = nn.Softmax(dim=-1)
|
||||
self.dropout = nn.Dropout(dropout)
|
||||
self.temperature = nn.Parameter(torch.ones([1, heads, 1, 1]) * 0.5)
|
||||
self.H = H
|
||||
self.W = W
|
||||
self.D = D
|
||||
|
||||
self.in_project_x = nn.Conv3d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
self.in_project_fx = nn.Conv3d(dim, inner_dim, kernel, 1, kernel // 2)
|
||||
self.in_project_slice = nn.Linear(dim_head, slice_num)
|
||||
for l in [self.in_project_slice]:
|
||||
torch.nn.init.orthogonal_(l.weight) # use a principled initialization
|
||||
self.to_q = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_k = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_v = nn.Linear(dim_head, dim_head, bias=False)
|
||||
self.to_out = nn.Sequential(
|
||||
nn.Linear(inner_dim, dim),
|
||||
nn.Dropout(dropout)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
# B N C
|
||||
B, N, C = x.shape
|
||||
x = x.reshape(B, self.H, self.W, self.D, C).contiguous().permute(0, 4, 1, 2, 3).contiguous() # B C H W
|
||||
|
||||
### (1) Slice
|
||||
fx_mid = self.in_project_fx(x).permute(0, 2, 3, 4, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N C
|
||||
x_mid = self.in_project_x(x).permute(0, 2, 3, 4, 1).contiguous().reshape(B, N, self.heads, self.dim_head) \
|
||||
.permute(0, 2, 1, 3).contiguous() # B H N G
|
||||
slice_weights = self.softmax(
|
||||
self.in_project_slice(x_mid) / torch.clamp(self.temperature, min=0.1, max=5)) # B H N G
|
||||
slice_norm = slice_weights.sum(2) # B H G
|
||||
slice_token = torch.einsum("bhnc,bhng->bhgc", fx_mid, slice_weights)
|
||||
slice_token = slice_token / ((slice_norm + 1e-5)[:, :, :, None].repeat(1, 1, 1, self.dim_head))
|
||||
|
||||
### (2) Attention among slice tokens
|
||||
q_slice_token = self.to_q(slice_token)
|
||||
k_slice_token = self.to_k(slice_token)
|
||||
v_slice_token = self.to_v(slice_token)
|
||||
dots = torch.matmul(q_slice_token, k_slice_token.transpose(-1, -2)) * self.scale
|
||||
attn = self.softmax(dots)
|
||||
attn = self.dropout(attn)
|
||||
out_slice_token = torch.matmul(attn, v_slice_token) # B H G D
|
||||
|
||||
### (3) Deslice
|
||||
out_x = torch.einsum("bhgc,bhng->bhnc", out_slice_token, slice_weights)
|
||||
out_x = rearrange(out_x, 'b h n d -> b n (h d)')
|
||||
return self.to_out(out_x)
|
||||
@@ -0,0 +1,158 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from timm.models.layers import trunc_normal_
|
||||
from model.Embedding import timestep_embedding
|
||||
import numpy as np
|
||||
from model.Physics_Attention import Physics_Attention_Irregular_Mesh
|
||||
|
||||
ACTIVATION = {'gelu': nn.GELU, 'tanh': nn.Tanh, 'sigmoid': nn.Sigmoid, 'relu': nn.ReLU, 'leaky_relu': nn.LeakyReLU(0.1),
|
||||
'softplus': nn.Softplus, 'ELU': nn.ELU, 'silu': nn.SiLU}
|
||||
|
||||
|
||||
class MLP(nn.Module):
|
||||
def __init__(self, n_input, n_hidden, n_output, n_layers=1, act='gelu', res=True):
|
||||
super(MLP, self).__init__()
|
||||
|
||||
if act in ACTIVATION.keys():
|
||||
act = ACTIVATION[act]
|
||||
else:
|
||||
raise NotImplementedError
|
||||
self.n_input = n_input
|
||||
self.n_hidden = n_hidden
|
||||
self.n_output = n_output
|
||||
self.n_layers = n_layers
|
||||
self.res = res
|
||||
self.linear_pre = nn.Sequential(nn.Linear(n_input, n_hidden), act())
|
||||
self.linear_post = nn.Linear(n_hidden, n_output)
|
||||
self.linears = nn.ModuleList([nn.Sequential(nn.Linear(n_hidden, n_hidden), act()) for _ in range(n_layers)])
|
||||
|
||||
def forward(self, x):
|
||||
x = self.linear_pre(x)
|
||||
for i in range(self.n_layers):
|
||||
if self.res:
|
||||
x = self.linears[i](x) + x
|
||||
else:
|
||||
x = self.linears[i](x)
|
||||
x = self.linear_post(x)
|
||||
return x
|
||||
|
||||
|
||||
class Transolver_block(nn.Module):
|
||||
"""Transformer encoder block."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
num_heads: int,
|
||||
hidden_dim: int,
|
||||
dropout: float,
|
||||
act='gelu',
|
||||
mlp_ratio=4,
|
||||
last_layer=False,
|
||||
out_dim=1,
|
||||
slice_num=32,
|
||||
):
|
||||
super().__init__()
|
||||
self.last_layer = last_layer
|
||||
self.ln_1 = nn.LayerNorm(hidden_dim)
|
||||
self.Attn = Physics_Attention_Irregular_Mesh(hidden_dim, heads=num_heads, dim_head=hidden_dim // num_heads,
|
||||
dropout=dropout, slice_num=slice_num)
|
||||
self.ln_2 = nn.LayerNorm(hidden_dim)
|
||||
self.mlp = MLP(hidden_dim, hidden_dim * mlp_ratio, hidden_dim, n_layers=0, res=False, act=act)
|
||||
if self.last_layer:
|
||||
self.ln_3 = nn.LayerNorm(hidden_dim)
|
||||
self.mlp2 = nn.Linear(hidden_dim, out_dim)
|
||||
|
||||
def forward(self, fx):
|
||||
fx = self.Attn(self.ln_1(fx)) + fx
|
||||
fx = self.mlp(self.ln_2(fx)) + fx
|
||||
if self.last_layer:
|
||||
return self.mlp2(self.ln_3(fx))
|
||||
else:
|
||||
return fx
|
||||
|
||||
|
||||
class Model(nn.Module):
|
||||
def __init__(self,
|
||||
space_dim=1,
|
||||
n_layers=5,
|
||||
n_hidden=256,
|
||||
dropout=0.0,
|
||||
n_head=8,
|
||||
Time_Input=False,
|
||||
act='gelu',
|
||||
mlp_ratio=1,
|
||||
fun_dim=1,
|
||||
out_dim=1,
|
||||
slice_num=32,
|
||||
ref=8,
|
||||
unified_pos=False
|
||||
):
|
||||
super(Model, self).__init__()
|
||||
self.__name__ = 'Transolver_1D'
|
||||
self.ref = ref
|
||||
self.unified_pos = unified_pos
|
||||
self.Time_Input = Time_Input
|
||||
self.n_hidden = n_hidden
|
||||
self.space_dim = space_dim
|
||||
if self.unified_pos:
|
||||
self.preprocess = MLP(fun_dim + self.ref * self.ref, n_hidden * 2, n_hidden, n_layers=0, res=False, act=act)
|
||||
else:
|
||||
self.preprocess = MLP(fun_dim + space_dim, n_hidden * 2, n_hidden, n_layers=0, res=False, act=act)
|
||||
if Time_Input:
|
||||
self.time_fc = nn.Sequential(nn.Linear(n_hidden, n_hidden), nn.SiLU(), nn.Linear(n_hidden, n_hidden))
|
||||
|
||||
self.blocks = nn.ModuleList([Transolver_block(num_heads=n_head, hidden_dim=n_hidden,
|
||||
dropout=dropout,
|
||||
act=act,
|
||||
mlp_ratio=mlp_ratio,
|
||||
out_dim=out_dim,
|
||||
slice_num=slice_num,
|
||||
last_layer=(_ == n_layers - 1))
|
||||
for _ in range(n_layers)])
|
||||
self.initialize_weights()
|
||||
self.placeholder = nn.Parameter((1 / (n_hidden)) * torch.rand(n_hidden, dtype=torch.float))
|
||||
|
||||
def initialize_weights(self):
|
||||
self.apply(self._init_weights)
|
||||
|
||||
def _init_weights(self, m):
|
||||
if isinstance(m, nn.Linear):
|
||||
trunc_normal_(m.weight, std=0.02)
|
||||
if isinstance(m, nn.Linear) and m.bias is not None:
|
||||
nn.init.constant_(m.bias, 0)
|
||||
elif isinstance(m, (nn.LayerNorm, nn.BatchNorm1d)):
|
||||
nn.init.constant_(m.bias, 0)
|
||||
nn.init.constant_(m.weight, 1.0)
|
||||
|
||||
def get_grid(self, x, batchsize=1):
|
||||
# x: B N 2
|
||||
# grid_ref
|
||||
gridx = torch.tensor(np.linspace(0, 1, self.ref), dtype=torch.float)
|
||||
gridx = gridx.reshape(1, self.ref, 1, 1).repeat([batchsize, 1, self.ref, 1])
|
||||
gridy = torch.tensor(np.linspace(0, 1, self.ref), dtype=torch.float)
|
||||
gridy = gridy.reshape(1, 1, self.ref, 1).repeat([batchsize, self.ref, 1, 1])
|
||||
grid_ref = torch.cat((gridx, gridy), dim=-1).cuda().reshape(batchsize, self.ref * self.ref, 2) # B H W 8 8 2
|
||||
|
||||
pos = torch.sqrt(torch.sum((x[:, :, None, :] - grid_ref[:, None, :, :]) ** 2, dim=-1)). \
|
||||
reshape(batchsize, x.shape[1], self.ref * self.ref).contiguous()
|
||||
return pos
|
||||
|
||||
def forward(self, x, fx, T=None):
|
||||
if self.unified_pos:
|
||||
x = self.get_grid(x, x.shape[0])
|
||||
if fx is not None:
|
||||
fx = torch.cat((x, fx), -1)
|
||||
fx = self.preprocess(fx)
|
||||
else:
|
||||
fx = self.preprocess(x)
|
||||
fx = fx + self.placeholder[None, None, :]
|
||||
|
||||
if T is not None:
|
||||
Time_emb = timestep_embedding(T, self.n_hidden).repeat(1, x.shape[1], 1)
|
||||
Time_emb = self.time_fc(Time_emb)
|
||||
fx = fx + Time_emb
|
||||
|
||||
for block in self.blocks:
|
||||
fx = block(fx)
|
||||
|
||||
return fx
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"revision": "75e0f67643806a81cd1d3f6adc88dd8c02416fe7",
|
||||
"repository": "https://github.com/thuml/Transolver",
|
||||
"files": {
|
||||
"LICENSE": "2c919cd03fa823bf7eefc00a957ff8324c865cd22aa5285e563dc4b558084f25",
|
||||
"PDE-Solving-StandardBenchmark/model/Transolver_Irregular_Mesh.py": "5259759f7ecc6a954c8442ac8359990b9f242f1437c452c69e5e0f03549b5311",
|
||||
"PDE-Solving-StandardBenchmark/model/Physics_Attention.py": "4fc710f6f96788fb59cb19fab8d64873d3d16e9e182f12a544d451c74c548cc7"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user