| import torch |
|
|
| |
| def filter_logits_with_candidates(logits, candidate_indices): |
| """ |
| 高效版本:使用向量化索引替代for循环,提升计算效率 |
| """ |
| |
| filtered_logits = torch.full_like(logits, float('-inf')) |
| |
| |
| batch_size = logits.size(0) |
| num_candidates = candidate_indices.size(1) |
| |
| |
| batch_indices = torch.arange(batch_size, device=logits.device)[:, None].expand(-1, num_candidates) |
| print(batch_indices) |
| |
| filtered_logits[batch_indices, candidate_indices] = logits[batch_indices, candidate_indices] |
| |
| return filtered_logits |
|
|
| |
| |
| |
| logits = torch.tensor([ |
| [1.2, 3.4, 0.5, 2.1, 4.3, 1.8, 0.9, 3.7, 2.5, 1.1], |
| [2.8, 0.7, 3.1, 1.5, 4.0, 2.2, 3.5, 0.3, 1.9, 2.6] |
| ]) |
|
|
| |
| |
| candidate_indices = torch.tensor([ |
| [2, 5, 7], |
| [1, 4, 8] |
| ]) |
|
|
| |
| filtered_logits = filter_logits_with_candidates(logits, candidate_indices) |
|
|
| |
| print("原始logits(shape={}):".format(logits.shape)) |
| print(logits) |
| print("\n候选索引(shape={}):".format(candidate_indices.shape)) |
| print(candidate_indices) |
| print("\n过滤后logits(仅候选索引位置保留原值,其余为-inf):") |
| print(filtered_logits) |