You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无循环实现多维PyTorch张量交集索引:GPU高效处理方案问询

解决方案

一、一维张量索引匹配(精简版场景)

针对一维张量t_1和t_2,需高效找到t_2元素在t_1中的对应索引,全程GPU执行、无循环、适配超大张量:

实现代码

import torch

# 初始化GPU设备
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 示例张量(确保为long类型,避免浮点精度问题)
t_1 = torch.Tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]).long().to(device)
t_2 = torch.Tensor([1, 5, 7]).long().to(device)

# 1. 提取t_1的唯一值及原索引映射
unique_vals, inverse_indices = torch.unique(t_1, return_inverse=True)

# 2. 构建值到unique索引的映射张量(GPU上高效查找)
val_to_unique_idx = torch.zeros(unique_vals.max() + 1, dtype=torch.long, device=device)
val_to_unique_idx[unique_vals] = torch.arange(len(unique_vals), device=device)

# 3. 映射t_2元素到原t_1的索引
t_2_unique_indices = val_to_unique_idx[t_2]
output = inverse_indices[t_2_unique_indices]

print(output)  # 输出: tensor([0, 4, 6], device='cuda:0')

核心优势

  • 全程GPU并行操作,无Python循环;
  • 内存开销为O(F)(F为t_1长度),适合百万级以上超大张量;
  • 基于张量索引的O(1)查找,效率远高于广播匹配。

二、三角面张量匹配(详细版场景)

针对三角网格面数据,需忽略顶点顺序匹配t_2中每个面在t_1中的索引,核心思路是先统一面的顶点顺序,再转换为一维键实现高效匹配:

实现代码

import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 示例数据:t_1为Fx3的面数据,t_2为Nx3的候选面
t_1 = torch.tensor([[0,1,2], [3,4,5], [2,0,1], [6,7,8]], dtype=torch.long).to(device)
t_2 = torch.tensor([[2,0,1], [6,7,8]], dtype=torch.long).to(device)

# 步骤1:对每个面的顶点排序,消除顺序差异
t_1_sorted, _ = torch.sort(t_1, dim=1)
t_2_sorted, _ = torch.sort(t_2, dim=1)

# 步骤2:将排序后的面转换为唯一一维键(避免哈希冲突,适合顶点索引范围可控的场景)
max_vertex_idx = t_1.max().item() + 1
weight = torch.tensor([max_vertex_idx**2, max_vertex_idx, 1], dtype=torch.long, device=device)
t_1_keys = torch.sum(t_1_sorted * weight, dim=1)
t_2_keys = torch.sum(t_2_sorted * weight, dim=1)

# 步骤3:复用一维张量的高效匹配逻辑
unique_keys, inverse_indices = torch.unique(t_1_keys, return_inverse=True)
key_to_unique_idx = torch.zeros(unique_keys.max() + 1, dtype=torch.long, device=device)
key_to_unique_idx[unique_keys] = torch.arange(len(unique_keys), device=device)

t_2_key_indices = key_to_unique_idx[t_2_keys]
output = inverse_indices[t_2_key_indices]

print(output)  # 输出: tensor([0, 3], device='cuda:0')

备选哈希方案(适用于超大顶点索引)

若顶点索引范围过大(如超过1e5),整数编码易溢出,可改用哈希函数生成一维键:

def hash_faces(faces):
    # GPU并行计算的哈希函数,避免溢出
    h = torch.zeros(faces.shape[0], dtype=torch.int64, device=faces.device)
    h = h ^ faces[:,0] * 313
    h = h ^ faces[:,1] * 971
    h = h ^ faces[:,2] * 1999
    return h

t_1_keys = hash_faces(t_1_sorted)
t_2_keys = hash_faces(t_2_sorted)

核心优势

  • 排序操作是PyTorch原生GPU加速操作,无循环;
  • 一维键转换将二维匹配问题降维,内存开销为O(F+N),适配超大F和N;
  • 完全基于PyTorch原生操作,无第三方依赖,可稳定运行在GPU环境。

内容的提问来源于stack exchange,提问作者tlk13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:35:04