-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ascend] use aclnnUnique2 impl unique #1314
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* @file | ||
* @author DeepLink | ||
* @copyright (c) 2024, DeepLink. | ||
*/ | ||
|
||
#include <cstdint> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "../aclnn/adaptor.hpp" | ||
|
||
namespace impl { | ||
namespace ascend { | ||
|
||
diopiError_t diopiUnique(diopiContextHandle_t ctx, diopiTensorHandle_t* out, diopiConstTensorHandle_t input, const int64_t* dim, bool sorted, bool returnCounts, | ||
diopiTensorHandle_t indices, diopiTensorHandle_t* counts) { | ||
// aclnnUnique2 only support dim == nullptr | ||
ASCEND_CHECK_ABORT(dim == nullptr, "dim is not supported in aclnnUnique2"); | ||
|
||
bool returnInverse = (indices != nullptr) ? true : false; | ||
AscendTensor inputAt(input); | ||
const std::vector<int64_t>& inSizeVec = inputAt.shape(); | ||
diopiSize_t inSize = {inSizeVec.data(), static_cast<int64_t>(inSizeVec.size())}; | ||
std::vector<int64_t> numelSizeVec{inputAt.numel()}; | ||
diopiSize_t numelSize = {numelSizeVec.data(), static_cast<int64_t>(numelSizeVec.size())}; | ||
std::vector<int64_t> zeroSizeVec = {0}; | ||
diopiSize_t zeroSize = {zeroSizeVec.data(), 1}; | ||
|
||
// allocate temp out tensor | ||
diopiTensorHandle_t outTmp = nullptr; | ||
if (dim) { | ||
diopiRequireTensor(ctx, &outTmp, &inSize, nullptr, inputAt.dtype(), diopi_device); | ||
} else { | ||
diopiRequireTensor(ctx, &outTmp, &numelSize, nullptr, inputAt.dtype(), diopi_device); | ||
} | ||
|
||
// allocate temp inverse tensor | ||
diopiTensorHandle_t inverseTmp = nullptr; | ||
if (returnInverse || returnCounts) { | ||
diopiRequireTensor(ctx, &inverseTmp, &inSize, nullptr, diopi_dtype_int64, diopi_device); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 参考camb的requiresTensor写一些函数通用函数(华为上叫makeTensor),这样就不用每次都从diopiSize_t来获取tensor |
||
} else { | ||
diopiRequireTensor(ctx, &inverseTmp, &zeroSize, nullptr, diopi_dtype_int64, diopi_device); | ||
} | ||
|
||
// allocate temp counts tensor | ||
diopiTensorHandle_t countsTmp = nullptr; | ||
if (returnCounts) { | ||
diopiRequireTensor(ctx, &countsTmp, &numelSize, nullptr, diopi_dtype_int64, diopi_device); | ||
} else { | ||
diopiRequireTensor(ctx, &countsTmp, &zeroSize, nullptr, diopi_dtype_int64, diopi_device); | ||
} | ||
|
||
// call aclnnUnique2 | ||
auto params = ::impl::ascend::aclnn_adaptor::convertParams(input, sorted, returnInverse, returnCounts, outTmp, inverseTmp, countsTmp).params(); | ||
DIOPI_ASECND_CALL_ACLNN_TYPE_SYNC(aclnnUnique2, ctx, params); | ||
|
||
// get true outShape by aclGetViewShape | ||
int64_t* viewDims = nullptr; | ||
uint64_t viewDimNum = 0; | ||
using aclGetViewShapeFunc = int (*)(const aclTensor* tensor, int64_t** viewDims, uint64_t* viewDimsNum); | ||
static aclGetViewShapeFunc aclGetViewShape = reinterpret_cast<aclGetViewShapeFunc>(impl::ascend::aclnn_adaptor::getOpApiFuncAddr("aclGetViewShape")); | ||
int ret = aclGetViewShape(std::get<4>(params), &viewDims, &viewDimNum); | ||
ASCEND_CHECK_ABORT(ret == 0, "get out aclGetViewShape failed"); | ||
|
||
// fill out tensor | ||
diopiSize_t outShape{viewDims, static_cast<int64_t>(viewDimNum)}; | ||
diopiRequireTensor(ctx, out, &outShape, nullptr, inputAt.dtype(), diopi_device); | ||
AscendTensor outAt(*out); | ||
AscendTensor outTmpAt(outTmp); | ||
outTmpAt.view({outShape.data, outShape.data + outShape.len}); | ||
DIOPI_ASCEND_CALL_ACLNN(aclnnInplaceCopy, ctx, outAt, outTmpAt); | ||
|
||
// fill indices tensor | ||
if (returnInverse) { | ||
AscendTensor inverseTmpAt(inverseTmp); | ||
|
||
diopiSize_t inSize = {inverseTmpAt.shape().data(), static_cast<int64_t>(inverseTmpAt.shape().size())}; | ||
AscendTensor indicesTmpAt(indices); | ||
if (indicesTmpAt.shape() != inverseTmpAt.shape()) { | ||
diopiRequireTensor(ctx, &indices, &inSize, nullptr, diopi_dtype_int64, diopi_device); | ||
} | ||
AscendTensor indicesAt(indices); | ||
DIOPI_ASCEND_CALL_ACLNN(aclnnInplaceCopy, ctx, indicesAt, inverseTmpAt); | ||
} | ||
|
||
// fill counts tensor | ||
if (returnCounts) { | ||
AscendTensor countsTmpAt(countsTmp); | ||
int ret2 = aclGetViewShape(std::get<6>(params), &viewDims, &viewDimNum); | ||
ASCEND_CHECK_ABORT(ret2 == 0, "get count aclGetViewShape failed"); | ||
diopiSize_t countShape{viewDims, static_cast<int64_t>(viewDimNum)}; | ||
diopiRequireTensor(ctx, counts, &countShape, nullptr, countsTmpAt.dtype(), diopi_device); | ||
AscendTensor countsAt(*counts); | ||
countsTmpAt.view({countShape.data, countShape.data + countShape.len}); | ||
|
||
DIOPI_ASCEND_CALL_ACLNN(aclnnInplaceCopy, ctx, countsAt, countsTmpAt); | ||
} | ||
|
||
// delete viewDims pointer | ||
if (viewDims) { | ||
delete viewDims; | ||
viewDims = nullptr; | ||
} | ||
|
||
return diopiSuccess; | ||
} | ||
|
||
} // namespace ascend | ||
} // namespace impl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
华为有aclnnUniqueDim来调用 带dim的unique.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯嗯,尝试用aclnnUniqueDim去实现支持dim的场景,不过有些测试用例没有通过,看模型中暂时使用的是unique2,先实现了不使用dim的算子,并且用ASCEND_CHECK_ABORT检查了dim。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在pr描述里写一下,并且在ASCEND_CHECK_ABORT这里写上可以调用aclnnUniqueDim.