// SPDX-License-Identifier: LGPL-3.0-or-later // // CUDA entry of deepmd::latent_charge_sum (schema and formulas in ops.cc). // // A call evaluates its frames by the direct sum when they carry no cell and // by the mesh sum when they do (charge_sum.h). Across the domains of a // decomposed system, the ranks gather the positions and charges of the charged // nodes of every domain into one frame, in rank order (domain_exchange.h), and // each rank evaluates its own charged nodes against that frame; the outputs // return to the node axis of the rank, where the nodes it does not own carry // no charge. #include #include #include #include #include #include #include #include #include "charge_sum.h" #include "domain_exchange.h" namespace { using deepmd::long_range::ChargeSumOutputs; using deepmd::long_range::kLes; using deepmd::long_range::kMaxChannels; using deepmd::long_range::kMaxGaussians; using deepmd::long_range::kSog; // The evaluation of the frames, by the direct or the mesh sum, of the owner // nodes [owner_begin, owner_begin + owner_count), which are every node or lie // in a single frame. ``box`` is defined for periodic frames only. Without // nodes or frames the outputs are zero. struct Evaluation { const torch::Tensor& box; const torch::Tensor& coefficient; int64_t kernel; int64_t channel; double prefactor; int64_t order; double spacing; bool periodic; int64_t mesh_backend; int64_t force_mode; ChargeSumOutputs operator()(const torch::Tensor& coord, const torch::Tensor& charge, const torch::Tensor& node_mask, const torch::Tensor& n_node_per_frame, const torch::Tensor& total_charge, int owner_begin, int owner_count, const torch::Tensor* communicator) const { const int64_t frame_count = n_node_per_frame.size(0); if (coord.size(0) == 0 || frame_count == 0) { return {torch::zeros({owner_count, 1}, coord.options()), torch::zeros({owner_count, charge.size(1)}, charge.options()), torch::zeros({owner_count, 3}, charge.options()), torch::zeros({owner_count, 9}, charge.options()), torch::zeros({frame_count, 9}, charge.options())}; } TORCH_CHECK( (owner_begin == 0 && owner_count == coord.size(0)) || frame_count == 1, "latent_charge_sum: owners other than every node lie in one " "frame"); if (periodic) { if (mesh_backend == 1 || mesh_backend == 2) { return deepmd::long_range::midtown_sum( coord, charge, node_mask, n_node_per_frame, total_charge, box, coefficient, kernel, channel, prefactor, order, spacing, owner_begin, owner_count, communicator, force_mode, /*quads=*/mesh_backend == 2); } TORCH_CHECK(mesh_backend == 0, "latent_charge_sum: unknown mesh backend code ", mesh_backend, "; expected 0 (bspline), 1 (midtown) or 2 (quads)"); TORCH_CHECK(force_mode == 0, "latent_charge_sum: force_mode != 0 (ik) needs the midtown " "backend"); return deepmd::long_range::mesh_sum( coord, charge, node_mask, n_node_per_frame, total_charge, box, coefficient, kernel, channel, prefactor, order, spacing, owner_begin, owner_count, communicator); } return deepmd::long_range::direct_sum( coord, charge, node_mask, n_node_per_frame, total_charge, coefficient, kernel, channel, prefactor, owner_begin, owner_count, communicator); } }; // Evaluates the frame of a system decomposed into the domains of // ``communicator``, of which this rank holds one: its charged nodes are its // owners, the charged nodes of every domain its partners. The nodes the rank // does not own carry no charge, and the per-frame virial is the share of its // owners. ChargeSumOutputs evaluate_across_domains(const Evaluation& evaluation, const torch::Tensor& coord, const torch::Tensor& charge, const torch::Tensor& node_mask, const torch::Tensor& n_node_per_frame, const torch::Tensor& total_charge, const torch::Tensor& communicator) { TORCH_CHECK(n_node_per_frame.size(0) == 1, "latent_charge_sum: an evaluation across MPI ranks takes one " "frame, got ", n_node_per_frame.size(0)); const int64_t node_count = coord.size(0); // The charged nodes of the frame; the padding nodes past it carry none. // Their count is the one value the host has to wait for before the // exchange. const auto in_frame = torch::arange(node_count, n_node_per_frame.options()) < n_node_per_frame; const auto owned = torch::nonzero(node_mask & in_frame).squeeze(1); const auto rows = torch::cat({coord.index_select(0, owned), charge.index_select(0, owned).to(torch::kFloat64)}, 1) .contiguous(); const auto [partners, owner_begin] = deepmd::long_range::gather_domain_rows(communicator, rows); const int64_t partner_count = partners.size(0); const auto partner_coord = partners.slice(1, 0, 3).contiguous(); const auto partner_charge = partners.slice(1, 3).to(torch::kFloat32).contiguous(); const auto partner_mask = torch::ones({partner_count}, node_mask.options()); const auto partner_frame = torch::full({1}, partner_count, n_node_per_frame.options()); const auto [owner_energy, owner_gradient, owner_force, owner_atom_virial, virial] = evaluation(partner_coord, partner_charge, partner_mask, partner_frame, total_charge, static_cast(owner_begin), static_cast(owned.size(0)), &communicator); return {torch::zeros({node_count, 1}, coord.options()) .index_copy_(0, owned, owner_energy), torch::zeros({node_count, charge.size(1)}, charge.options()) .index_copy_(0, owned, owner_gradient), torch::zeros({node_count, 3}, charge.options()) .index_copy_(0, owned, owner_force), torch::zeros({node_count, 9}, charge.options()) .index_copy_(0, owned, owner_atom_virial), virial}; } // Whether the frames carry cells: true when ``box`` is given and none of its // rows is zero, false when it is absent or all of its rows are; a call that // mixes the two is rejected. bool periodic_frames(const c10::optional& box) { if (!box.has_value()) { return false; } const auto cells = box->contiguous(); const double* value = cells.data_ptr(); const int64_t frame_count = cells.size(0); int64_t periodic = 0; for (int64_t f = 0; f < frame_count; ++f) { periodic += std::any_of(value + 9 * f, value + 9 * (f + 1), [](double entry) { return entry != 0.0; }); } TORCH_CHECK(periodic == 0 || periodic == frame_count, "latent_charge_sum: every frame of a call is periodic, or none " "is"); return periodic > 0; } } // namespace namespace deepmd { namespace long_range { ChargeSumOutputs latent_charge_sum(torch::Tensor coord, torch::Tensor charge, torch::Tensor node_mask, torch::Tensor n_node_per_frame, torch::Tensor total_charge, c10::optional box, torch::Tensor coefficient, torch::Tensor communicator, int64_t kernel, int64_t channel, double prefactor, int64_t order, double spacing, int64_t mesh_backend, int64_t force_mode) { // Both midtown families (CubeS2 = 1, QuadS = 2) read the kernel coefficients // on the host, so they take them as a CPU tensor and never synchronize to get // them. The B-spline backend evaluates them inside its device kernels and // needs them on the device. const bool coefficients_on_host = mesh_backend != 0; TORCH_CHECK(coord.is_cuda() && charge.is_cuda() && node_mask.is_cuda() && n_node_per_frame.is_cuda() && total_charge.is_cuda() && (coefficient.is_cuda() || coefficients_on_host), "latent_charge_sum: inputs must be CUDA tensors, except box and " "communicator"); TORCH_CHECK( charge.device() == coord.device() && node_mask.device() == coord.device() && n_node_per_frame.device() == coord.device() && total_charge.device() == coord.device() && (coefficient.device() == coord.device() || coefficients_on_host), "latent_charge_sum: inputs must share one device"); TORCH_CHECK(coord.dim() == 2 && coord.size(1) == 3 && coord.scalar_type() == torch::kFloat64, "latent_charge_sum: coord must be float64 with shape (N, 3)"); TORCH_CHECK(charge.dim() == 2 && charge.size(0) == coord.size(0) && charge.scalar_type() == torch::kFloat32, "latent_charge_sum: charge must be float32 with shape (N, C)"); TORCH_CHECK(node_mask.dim() == 1 && node_mask.size(0) == coord.size(0) && node_mask.scalar_type() == torch::kBool, "latent_charge_sum: node_mask must be bool with shape (N,)"); TORCH_CHECK(n_node_per_frame.dim() == 1, "latent_charge_sum: n_node_per_frame must be one-dimensional"); TORCH_CHECK(total_charge.dim() == 1 && total_charge.size(0) == n_node_per_frame.size(0) && total_charge.scalar_type() == torch::kFloat64, "latent_charge_sum: total_charge must be float64 with one entry " "per frame"); TORCH_CHECK( !box.has_value() || (box->device().is_cpu() && box->scalar_type() == torch::kFloat64 && box->dim() == 2 && box->size(0) == n_node_per_frame.size(0) && box->size(1) == 9), "latent_charge_sum: box must be a float64 CPU tensor with shape " "(F, 9)"); TORCH_CHECK( coefficient.dim() == 1 && coefficient.scalar_type() == torch::kFloat64, "latent_charge_sum: coefficient must be a float64 vector"); const int64_t channels = charge.size(1); TORCH_CHECK(channels >= 1 && channels <= kMaxChannels, "latent_charge_sum: the channel count must lie in [1, ", kMaxChannels, "], got ", channels); TORCH_CHECK(channel >= -1 && channel < channels, "latent_charge_sum: the constrained channel must be -1 or lie " "in [0, ", channels, "), got ", channel); if (kernel == kLes) { TORCH_CHECK(coefficient.numel() == 1, "latent_charge_sum: the erf kernel takes one coefficient"); } else if (kernel == kSog) { TORCH_CHECK(coefficient.numel() % 2 == 0 && coefficient.numel() > 0 && coefficient.numel() <= 2 * kMaxGaussians, "latent_charge_sum: the Gaussian kernel takes two " "coefficients per Gaussian, for at most ", kMaxGaussians, " Gaussians"); } else { TORCH_CHECK(false, "latent_charge_sum: unknown kernel ", kernel); } TORCH_CHECK(coord.size(0) < std::numeric_limits::max(), "latent_charge_sum: too many nodes"); const c10::cuda::CUDAGuard guard(coord.device()); coord = coord.contiguous(); charge = charge.contiguous(); node_mask = node_mask.contiguous(); total_charge = total_charge.contiguous(); const bool periodic = periodic_frames(box); const torch::Tensor cells = periodic ? box->contiguous() : torch::Tensor(); coefficient = coefficient.contiguous(); if (!periodic && !coefficient.is_cuda()) { // direct_sum reads the coefficients inside CUDA device kernels. coefficient = coefficient.to(coord.device()); } const Evaluation evaluation{cells, coefficient, kernel, channel, prefactor, order, spacing, periodic, mesh_backend, force_mode}; const int64_t ranks = deepmd::long_range::domain_count(communicator); // The z-slab distributed solve runs behind an opt-in switch. It now serves // every mesh backend: CubeS2 (1), QuadS (2) and the cardinal B-spline (0), // all through the same two-transpose FFT pipeline, each with its own // deconvolution (variance subtraction, exact separable window, or the // B-spline assignment factor) built by build_green_slab_half. const char* slab_env = std::getenv("SOG_MIDTOWN_SLAB"); bool slab_ok = periodic && slab_env != nullptr && std::atoi(slab_env) != 0 && (mesh_backend == 0 || mesh_backend == 1 || mesh_backend == 2); if (slab_ok && force_mode != 0) { // The distributed solve carries the ik force mode behind its own opt-in // switch: ik needs one extra transpose + inverse z per Cartesian component, // ~4x the collective traffic of the AD solve and one extra pencil and // brick, so it must not follow a SOG_MIDTOWN_SLAB that a bench harness or // a container profile set process-wide. const char* ik_env = std::getenv("SOG_SLAB_IK"); const bool ik_slab = ik_env != nullptr && std::atoi(ik_env) != 0; if (!ik_slab) { // Falling back to the replicated solve is a PARALLELISM change, not a // physics one -- the replicated solve evaluates the same force law -- so // warn and continue instead of aborting an MD run from an env var. Each // rank then holds the full mesh, which is why SOG_SLAB_STRICT=1 restores // the hard error for batch runs that must not silently take the more // memory-hungry path. const char* strict = std::getenv("SOG_SLAB_STRICT"); TORCH_CHECK( strict == nullptr || std::atoi(strict) == 0, "latent_charge_sum: force_mode != 0 (ik) on the distributed mesh " "needs SOG_SLAB_IK=1; unset SOG_MIDTOWN_SLAB (or unset " "SOG_SLAB_STRICT to fall back automatically)"); static bool warned_ik_slab = false; if (!warned_ik_slab) { warned_ik_slab = true; TORCH_WARN( "SOG_MIDTOWN_SLAB is set but force_mode != 0 (ik) and " "SOG_SLAB_IK is not: this call runs the REPLICATED solve, so every " "rank holds the full mesh. Set SOG_SLAB_IK=1 to run ik on the " "distributed mesh. The force law is identical either way."); } slab_ok = false; } else { TORCH_CHECK(mesh_backend != 0, "latent_charge_sum: the ik force mode is defined for the " "midtown and quads mesh backends only, not the B-spline"); } } if (slab_ok) { TORCH_CHECK(periodic, "the slab backend needs periodic frames"); // The B-spline slab builds its green table from the SOG bands, so it serves // the SOG kernel only (the replicated mesh_sum carries both LES and SOG). TORCH_CHECK(mesh_backend != 0 || kernel == kSog, "the slab B-spline backend evaluates the SOG kernel only"); return deepmd::long_range::midtown_sum_slab( coord, charge, node_mask, n_node_per_frame, total_charge, cells, coefficient, kernel, channel, prefactor, order, spacing, &communicator, /*quads=*/mesh_backend == 2, /*bspline=*/mesh_backend == 0, force_mode); } const char* dist_env = std::getenv("SOG_MIDTOWN_DIST"); const bool want_dist = dist_env != nullptr && std::atoi(dist_env) != 0; // The local-spread distributed solve runs `sog_gpu_spread_f`/`solve_f`, whose // stencils are hardwired to CubeS2 order 6; it is not spline-parametric, so // it stays on the `midtown` backend and QuadS falls through to the replicated // (gathered) path rather than being served with the wrong stencil. if (mesh_backend == 1 && ranks > 1 && want_dist && force_mode == 0) { // The midtown mesh spreads this rank's atoms itself and sums the meshes // over the ranks, so it takes the caller's local arrays rather than the // gathered frame. TORCH_CHECK(periodic, "the distributed midtown backend needs periodic frames"); return deepmd::long_range::midtown_sum_local( coord, charge, node_mask, n_node_per_frame, total_charge, cells, coefficient, kernel, channel, prefactor, order, spacing, &communicator); } if (mesh_backend == 1 && ranks > 1 && want_dist && force_mode != 0) { // Same fallback as the slab branch above, but this one has no ik path at // all: the local-spread stencils are hardwired to CubeS2 order 6 and have // no Fourier-differentiated gather. const char* strict = std::getenv("SOG_SLAB_STRICT"); TORCH_CHECK( strict == nullptr || std::atoi(strict) == 0, "latent_charge_sum: force_mode != 0 (ik) is not implemented on the " "local-spread distributed solve; unset SOG_MIDTOWN_DIST (or unset " "SOG_SLAB_STRICT to fall back automatically)"); static bool warned_ik_dist = false; if (!warned_ik_dist) { warned_ik_dist = true; TORCH_WARN( "SOG_MIDTOWN_DIST is set but force_mode != 0 (ik): the local-spread " "solve has no Fourier-differentiated force, so this call runs the " "replicated solve. The force law is identical; every rank now holds " "the full mesh. (The z-slab solve does implement ik: " "SOG_MIDTOWN_SLAB=1 SOG_SLAB_IK=1.)"); } } if (ranks == 1) { return evaluation(coord, charge, node_mask, n_node_per_frame, total_charge, 0, static_cast(coord.size(0)), nullptr); } return evaluate_across_domains(evaluation, coord, charge, node_mask, n_node_per_frame, total_charge, communicator); } } // namespace long_range } // namespace deepmd TORCH_LIBRARY_FRAGMENT(deepmd, library) { library.impl("latent_charge_sum", torch::kCUDA, &deepmd::long_range::latent_charge_sum); }