Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adaptor/codegen/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def prepare() -> Tuple[dict, str]:

impl_plugin = options.impl_plugin
base_device = options.base_device
assert(base_device is None or base_device == "" or base_device == "torch", f"invalid base_device:{base_device}")
assert base_device is None or base_device == "" or base_device == "torch", f"invalid base_device:{base_device}"
if base_device == "":
base_device = None
def create_if_not_exist(name):
Expand Down
13 changes: 12 additions & 1 deletion impl/ascend/device_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,15 +911,26 @@
para=dict(
accumulate=[Skip(False),],
),
tensor_para=dict(
args=[
{
"ins": ['input'],
"shape": [Skip((16, 4, 4)),],
},
]
),
),

'index_put_bool_indices_value': dict( # llm used
name=['index_put'],
para=dict(
accumulate=[Skip(False),],
),
tensor_para=dict(
args=[
{
"ins": ['input'],
"shape": [Skip((3, 2, 2, 20)),],
"shape": [Skip((3, 2, 2, 20)), Skip((4, 2, 2, 6, 2))],
},
]
),
Expand Down
34 changes: 18 additions & 16 deletions impl/ascend/functions/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace impl {
namespace ascend {

static std::vector<AscendTensor> castIntIndicesToLongIndices(diopiContextHandle_t ctx, std::vector<AscendTensor>& indices) {
namespace indexProcess {
std::vector<AscendTensor> castIntIndicesToLongIndices(diopiContextHandle_t ctx, std::vector<AscendTensor>& indices) {
std::vector<AscendTensor> result;
for (auto& t : indices) {
if (!t.defined()) {
Expand All @@ -37,7 +38,7 @@ static std::vector<AscendTensor> castIntIndicesToLongIndices(diopiContextHandle_
return result;
}

static void checkIndexTensorTypes(const std::vector<AscendTensor>& indices) {
void checkIndexTensorTypes(const std::vector<AscendTensor>& indices) {
for (const auto& t : indices) {
if (t.defined()) {
diopiDtype_t type = t.dtype();
Expand All @@ -47,7 +48,7 @@ static void checkIndexTensorTypes(const std::vector<AscendTensor>& indices) {
}
}

static AscendTensor nonZeroTensor(diopiContextHandle_t ctx, const AscendTensor& self) {
AscendTensor nonZeroTensor(diopiContextHandle_t ctx, const AscendTensor& self) {
int64_t numELem = self.numel() * self.dim();
std::vector<int64_t> nShape{self.numel(), self.dim()};
std::vector<int64_t> nStride(nShape.size(), 1);
Expand Down Expand Up @@ -86,14 +87,14 @@ static AscendTensor nonZeroTensor(diopiContextHandle_t ctx, const AscendTensor&
return AscendTensor(nzTrans);
}

static std::vector<AscendTensor> expandIndicesTensors(diopiContextHandle_t ctx, const AscendTensor& self, const std::vector<AscendTensor>& indices) {
std::vector<AscendTensor> expandIndicesTensors(diopiContextHandle_t ctx, const AscendTensor& self, const std::vector<AscendTensor>& indices) {
std::vector<AscendTensor> result;
for (auto& t : indices) {
if (!t.defined()) {
result.push_back(t);
} else {
if (t.dtype() == diopi_dtype_uint8 || t.dtype() == diopi_dtype_bool) {
ASCEND_CHECK(t.dtype() != diopi_dtype_uint8,
ASCEND_CHECK(t.dtype() == diopi_dtype_bool,
"indexing with dtype torch.uint8 is now deprecated,"
" please use a dtype torch.bool instead.");
for (uint64_t j = 0; j < static_cast<uint64_t>(t.dim()); j++) {
Expand All @@ -117,7 +118,7 @@ static std::vector<AscendTensor> expandIndicesTensors(diopiContextHandle_t ctx,
return result;
}

static aclTensor* createEmptyAclTensor() {
aclTensor* createEmptyAclTensor() {
std::vector<int64_t> nShape{0};
std::vector<int64_t> nStride{1};
int64_t storageSize = 0;
Expand Down Expand Up @@ -152,7 +153,7 @@ static std::vector<AscendTensor> indicesExpandedOutplace(std::vector<AscendTenso
return result;
}

static bool hasContiguousSubspace(std::vector<AscendTensor> indices) { // true if all the non-null tensors are adjacent
bool hasContiguousSubspace(std::vector<AscendTensor> indices) { // true if all the non-null tensors are adjacent
auto isDefined = [](const AscendTensor& tensor) { return tensor.defined(); };
auto isNull = [](const AscendTensor& tensor) { return !tensor.defined(); };
auto start = std::find_if(indices.begin(), indices.end(), isDefined);
Expand All @@ -161,7 +162,7 @@ static bool hasContiguousSubspace(std::vector<AscendTensor> indices) { // true
return it == stop.base();
}

static std::tuple<AscendTensor, std::vector<AscendTensor>> transposeToFront(AscendTensor self, std::vector<AscendTensor> indices) {
std::tuple<AscendTensor, std::vector<AscendTensor>> transposeToFront(AscendTensor self, std::vector<AscendTensor> indices) {
std::vector<int64_t> dims;
std::vector<AscendTensor> transposedIndices;

Expand All @@ -183,7 +184,7 @@ static std::tuple<AscendTensor, std::vector<AscendTensor>> transposeToFront(Asce
return std::make_tuple(self.permute(dims), transposedIndices);
}

static std::vector<int64_t> indexReshape(std::vector<AscendTensor> endIndices, int64_t dimsBefore, int64_t dimsAfter) {
std::vector<int64_t> indexReshape(std::vector<AscendTensor> endIndices, int64_t dimsBefore, int64_t dimsAfter) {
std::vector<int64_t> indexShape;
for (auto& idx : endIndices) {
if (idx.defined()) {
Expand All @@ -201,7 +202,7 @@ static std::vector<int64_t> indexReshape(std::vector<AscendTensor> endIndices, i
return indexShape;
}

static std::vector<int64_t> indexOutputSize(const AscendTensor& self, std::vector<AscendTensor>& indices) {
std::vector<int64_t> indexOutputSize(const AscendTensor& self, std::vector<AscendTensor>& indices) {
std::vector<AscendTensor> midIndices = indicesExpandedOutplace(indices);
while (midIndices.size() < (size_t)self.dim()) {
midIndices.emplace_back(nullptr);
Expand Down Expand Up @@ -254,6 +255,8 @@ static std::vector<int64_t> indexOutputSize(const AscendTensor& self, std::vecto
return outputSize;
}

} // namespace indexProcess

diopiError_t diopiIndex(diopiContextHandle_t ctx, diopiTensorHandle_t* out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t* indices, int64_t nums) {
AscendTensor inputAt(input);
std::vector<AscendTensor> indicesOrigin(nums);
Expand All @@ -263,13 +266,13 @@ diopiError_t diopiIndex(diopiContextHandle_t ctx, diopiTensorHandle_t* out, diop
}
}

std::vector<AscendTensor> indicesList = castIntIndicesToLongIndices(ctx, indicesOrigin);
checkIndexTensorTypes(indicesList);
std::vector<AscendTensor> indicesList = indexProcess::castIntIndicesToLongIndices(ctx, indicesOrigin);
indexProcess::checkIndexTensorTypes(indicesList);

auto indicesExpanded = expandIndicesTensors(ctx, inputAt, indicesList);
auto indicesExpanded = indexProcess::expandIndicesTensors(ctx, inputAt, indicesList);

std::vector<aclTensor*> allDefinedIndices;
auto emptyTensor = createEmptyAclTensor();
auto emptyTensor = indexProcess::createEmptyAclTensor();
for (const auto& idx : indicesExpanded) {
if (idx.defined()) {
allDefinedIndices.push_back(aclnn_adaptor::createAclTensorFromAscendTensor(idx));
Expand All @@ -278,8 +281,7 @@ diopiError_t diopiIndex(diopiContextHandle_t ctx, diopiTensorHandle_t* out, diop
}
}

std::vector<int64_t> outShape = indexOutputSize(inputAt, indicesExpanded);

std::vector<int64_t> outShape = indexProcess::indexOutputSize(inputAt, indicesExpanded);
diopiSize_t outSize = vectorToDiopiSize(outShape);
diopiRequireTensor(ctx, out, &outSize, nullptr, inputAt.dtype(), diopi_device);

Expand Down
62 changes: 58 additions & 4 deletions impl/ascend/functions/index_put.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,72 @@

namespace impl {
namespace ascend {

namespace indexProcess {
extern std::vector<AscendTensor> castIntIndicesToLongIndices(diopiContextHandle_t ctx, std::vector<AscendTensor>& indices);
extern void checkIndexTensorTypes(const std::vector<AscendTensor>& indices);
extern std::vector<AscendTensor> expandIndicesTensors(diopiContextHandle_t ctx, const AscendTensor& self, const std::vector<AscendTensor>& indices);
extern aclTensor* createEmptyAclTensor();
} // namespace indexProcess

diopiError_t diopiIndexPut(diopiContextHandle_t ctx, diopiTensorHandle_t out, diopiConstTensorHandle_t input, diopiConstTensorHandle_t values,
diopiConstTensorHandle_t* indices, int64_t indicesCounts, bool accumulate) {
diopiCopyInp(ctx, input, out);
std::vector<diopiConstTensorHandle_t> indicesVec(indices, indices + indicesCounts);
DIOPI_ASCEND_CALL_ACLNN(aclnnIndexPutImpl, ctx, out, indicesVec, values, accumulate, false);
AscendTensor inputAt(input);
AscendTensor valuesAt(values);
if (inputAt.numel() == 0 || valuesAt.numel() == 0) {
return diopiSuccess;
}
std::vector<AscendTensor> indicesOrigin(indicesCounts);
for (int64_t i = 0; i < indicesCounts; i++) {
if (indices[i] != nullptr) {
indicesOrigin[i] = AscendTensor(indices[i]);
}
}
std::vector<AscendTensor> indicesList = indexProcess::castIntIndicesToLongIndices(ctx, indicesOrigin);
indexProcess::checkIndexTensorTypes(indicesList);
auto indicesExpanded = indexProcess::expandIndicesTensors(ctx, inputAt, indicesList);
std::vector<aclTensor*> allDefinedIndices;
auto emptyTensor = indexProcess::createEmptyAclTensor();
for (const auto& idx : indicesExpanded) {
if (idx.defined()) {
allDefinedIndices.push_back(aclnn_adaptor::createAclTensorFromAscendTensor(idx));
} else {
allDefinedIndices.push_back(emptyTensor);
}
}

DIOPI_ASCEND_CALL_ACLNN(aclnnIndexPutImpl, ctx, out, allDefinedIndices, values, accumulate, false);
return diopiSuccess;
}

diopiError_t diopiIndexPutInp(diopiContextHandle_t ctx, diopiTensorHandle_t input, diopiConstTensorHandle_t values, diopiConstTensorHandle_t* indices,
int64_t indicesCounts, bool accumulate) {
std::vector<diopiConstTensorHandle_t> indicesVec(indices, indices + indicesCounts);
DIOPI_ASCEND_CALL_ACLNN(aclnnIndexPutImpl, ctx, input, indicesVec, values, accumulate, false);
AscendTensor inputAt(input);
AscendTensor valuesAt(values);
if (inputAt.numel() == 0 || valuesAt.numel() == 0) {
return diopiSuccess;
}
std::vector<AscendTensor> indicesOrigin(indicesCounts);
for (int64_t i = 0; i < indicesCounts; i++) {
if (indices[i] != nullptr) {
indicesOrigin[i] = AscendTensor(indices[i]);
}
}
std::vector<AscendTensor> indicesList = indexProcess::castIntIndicesToLongIndices(ctx, indicesOrigin);
indexProcess::checkIndexTensorTypes(indicesList);
auto indicesExpanded = indexProcess::expandIndicesTensors(ctx, inputAt, indicesList);
std::vector<aclTensor*> allDefinedIndices;
auto emptyTensor = indexProcess::createEmptyAclTensor();
for (const auto& idx : indicesExpanded) {
if (idx.defined()) {
allDefinedIndices.push_back(aclnn_adaptor::createAclTensorFromAscendTensor(idx));
} else {
allDefinedIndices.push_back(emptyTensor);
}
}

DIOPI_ASCEND_CALL_ACLNN(aclnnIndexPutImpl, ctx, input, allDefinedIndices, values, accumulate, false);
return diopiSuccess;
}

Expand Down