!3173 Broadcast of index.

Merge pull request !3173 from 王晓/index_broadcast_only_1.8
This commit is contained in:
i-robot
2023-05-16 12:48:11 +00:00
committed by Gitee
3 changed files with 34 additions and 1 deletions
+2 -1
View File
@@ -175,9 +175,10 @@ at::Tensor NPUNativeFunctions::index(const at::Tensor& self, const torch::List<c
at::native::checkIndexTensorTypes(orig);
auto indices = AdvanceIndex::npu_expand_tensors(self, orig);
auto broadcast_indices = AdvanceIndex::npu_broadcast_tensors(indices);
// not to transpose at all scene
return index_high_dims(self, indices);
return index_high_dims(self, broadcast_indices);
}
} // namespace native
@@ -223,5 +223,34 @@ std::vector<at::Tensor> AdvanceIndex::npu_expand_tensors(
return result;
}
std::vector<at::Tensor> AdvanceIndex::npu_broadcast_tensors(std::vector<at::Tensor> to_broadcast) {
// Broadcast a list of Tensors, ignoring undefined (null) tensors.
bool first = true;
std::vector<int64_t> sizes;
for (int i = 0; i < to_broadcast.size(); ++i) {
if (!to_broadcast[i].defined()) {
continue;
} else if (first) {
// The initial value of sizes is the first defined tensor's shape.
sizes = to_broadcast[i].sizes().vec();
first = false;
} else {
sizes = at::infer_size(sizes, to_broadcast[i].sizes());
}
}
std::vector<at::Tensor> result(to_broadcast.size());
for (int i = 0; i < to_broadcast.size(); ++i) {
if (!to_broadcast[i].defined()) {
continue;
} else if (to_broadcast[i].sizes().equals(sizes)) {
result[i] = to_broadcast[i];
} else {
result[i] = NPUNativeFunctions::npu_broadcast(to_broadcast[i], sizes);
}
}
return result;
}
} // namespace native
} // namespace at_npu
@@ -19,6 +19,8 @@
#include <ATen/native/IndexingUtils.h>
#include <ATen/ExpandUtils.h>
#include "torch_npu/csrc/framework/utils/NpuUtils.h"
namespace at_npu {
namespace native {
@@ -43,6 +45,7 @@ public:
static std::vector<at::Tensor> npu_expand_tensors(
const at::Tensor& self,
const torch::List<c10::optional<at::Tensor>>& indices);
static std::vector<at::Tensor> npu_broadcast_tensors(std::vector<at::Tensor> to_broadcast);
};
} // namespace native