-
Notifications
You must be signed in to change notification settings - Fork 77
Issue/259 add softmax operator #267
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
Open
Graylatzhou
wants to merge
8
commits into
InfiniTensor:main
Choose a base branch
from
Graylatzhou:softmax-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ab89fa8
Issue/259 add softmax operator
Graylatzhou 5fc6f55
Issue/259 修复CREATE宏定义中出现的问题
Graylatzhou 7583cea
Issue/259 修复存在的隐式类型转换
Graylatzhou e843a51
Issue/259 softmax_cpu计算抽象减少冗余
Graylatzhou 0ecfd1e
Issue/259 softmax_cuda 算子dispatch抽象以及格式规范化
Graylatzhou 56b4900
Issue/259 format code
Graylatzhou 6d64317
Issue/259 softmax算子测例修改以及添加bf16
Graylatzhou 581fd62
Issue/259 format code
Graylatzhou 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,20 @@ | ||
| #ifndef __INFINIOP_SOFTMAX_API_H__ | ||
| #define __INFINIOP_SOFTMAX_API_H__ | ||
|
|
||
| #include "../operator_descriptor.h" | ||
|
|
||
| typedef struct InfiniopDescriptor *infiniopSoftmaxDescriptor_t; | ||
|
|
||
| __C __export infiniStatus_t infiniopCreateSoftmaxDescriptor(infiniopHandle_t handle, | ||
| infiniopSoftmaxDescriptor_t *desc_ptr, | ||
| infiniopTensorDescriptor_t y_desc, | ||
| infiniopTensorDescriptor_t x_desc, | ||
| int axis); | ||
|
|
||
| __C infiniStatus_t infiniopGetSoftmaxWorkspaceSize(infiniopSoftmaxDescriptor_t desc, size_t *size); | ||
|
|
||
| __C infiniStatus_t infiniopSoftmax(infiniopSoftmaxDescriptor_t desc, void *workspace, size_t workspace_size, void *y, const void *x, void *stream); | ||
|
|
||
| __C infiniStatus_t infiniopDestroySoftmaxDescriptor(infiniopSoftmaxDescriptor_t desc); | ||
|
|
||
| #endif | ||
This file contains hidden or 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,94 @@ | ||
| #include "softmax_cpu.h" | ||
| #include "../../../devices/cpu/common_cpu.h" | ||
|
|
||
| namespace op::softmax::cpu { | ||
|
|
||
| Descriptor::~Descriptor() = default; | ||
|
|
||
| infiniStatus_t Descriptor::create( | ||
| infiniopHandle_t handle_, | ||
| Descriptor **desc_ptr, | ||
| infiniopTensorDescriptor_t y, | ||
| infiniopTensorDescriptor_t x, | ||
| int axis) { | ||
|
|
||
| auto handle = reinterpret_cast<device::cpu::Handle *>(handle_); | ||
| auto dtype = y->dtype(); | ||
|
|
||
| const auto &x_shape = x->shape(); | ||
| const auto &y_shape = y->shape(); | ||
|
|
||
| CHECK_DTYPE(dtype, INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_BF16); | ||
|
|
||
| CHECK_SAME_SHAPE(y_shape, x_shape); | ||
|
|
||
| auto result = SoftmaxInfo::create(y, x, axis); | ||
| CHECK_RESULT(result); | ||
|
|
||
| *desc_ptr = new Descriptor( | ||
| dtype, | ||
| result.take(), | ||
| 0, | ||
| nullptr, | ||
| handle->device, handle->device_id); | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| template <typename T> | ||
| void softmax_cpu(const SoftmaxInfo &info, | ||
| const void *x, void *y, int axis) { | ||
| int dim_size = info.dim_size; | ||
| int stride = info.stride; | ||
| int other_size = info.other_size; | ||
| auto input = reinterpret_cast<const T *>(x); | ||
| auto output = reinterpret_cast<T *>(y); | ||
|
|
||
| auto compute_softmax = [&](int i) { | ||
| int tid = i % stride + (i - i % stride) * dim_size; | ||
|
|
||
| float max_data = -INFINITY; | ||
| for (int j = 0; j < dim_size; j++) { | ||
| int index = tid + j * stride; | ||
| max_data = fmax(max_data, utils::cast<float>(input[index])); | ||
| } | ||
|
|
||
| float sum_data = 0.0f; | ||
| for (int j = 0; j < dim_size; j++) { | ||
| int index = tid + j * stride; | ||
| sum_data += std::exp(utils::cast<float>(input[index]) - max_data); | ||
| } | ||
|
|
||
| for (int j = 0; j < dim_size; j++) { | ||
| int index = tid + j * stride; | ||
| float result = std::exp(utils::cast<float>(input[index]) - max_data) / sum_data; | ||
| output[index] = utils::cast<T>(result); | ||
| } | ||
| }; | ||
| #pragma omp parallel for | ||
| for (int i = 0; i < other_size; i++) { | ||
| compute_softmax(i); | ||
| } | ||
| } | ||
|
|
||
| infiniStatus_t Descriptor::calculate( | ||
| void *workspace, | ||
| size_t workspace_size, | ||
| void *y, | ||
| const void *x, | ||
| void *stream_) const { | ||
| switch (_dtype) { | ||
| case INFINI_DTYPE_F16: | ||
| softmax_cpu<fp16_t>(_info, x, y, _info.axis); | ||
| return INFINI_STATUS_SUCCESS; | ||
| case INFINI_DTYPE_F32: | ||
| softmax_cpu<float>(_info, x, y, _info.axis); | ||
| return INFINI_STATUS_SUCCESS; | ||
| case INFINI_DTYPE_BF16: | ||
| softmax_cpu<bf16_t>(_info, x, y, _info.axis); | ||
| return INFINI_STATUS_SUCCESS; | ||
| default: | ||
| return INFINI_STATUS_BAD_TENSOR_DTYPE; | ||
| } | ||
| } | ||
| } // namespace op::softmax::cpu |
This file contains hidden or 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,8 @@ | ||
| #ifndef __SOFTMAX_CPU_H__ | ||
| #define __SOFTMAX_CPU_H__ | ||
|
|
||
| #include "../softmax.h" | ||
|
|
||
| DESCRIPTOR(cpu) | ||
|
|
||
| #endif // __SOFTMAX_CPU_H__ |
This file contains hidden or 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,54 @@ | ||
| #include "../../../devices/cuda/cuda_common.cuh" | ||
| #include "softmax_cuda.cuh" | ||
| #include "softmax_kernel.cuh" | ||
|
|
||
| namespace op::softmax::cuda { | ||
|
|
||
| struct Descriptor::Opaque { | ||
| std::shared_ptr<device::cuda::Handle::Internal> internal; | ||
| }; | ||
|
|
||
| Descriptor::~Descriptor() { | ||
| delete _opaque; | ||
| } | ||
|
|
||
| infiniStatus_t Descriptor::create( | ||
| infiniopHandle_t handle_, | ||
| Descriptor **desc_ptr, | ||
| infiniopTensorDescriptor_t y, | ||
| infiniopTensorDescriptor_t x, | ||
| int axis) { | ||
| auto dtype = y->dtype(); | ||
| auto handle = reinterpret_cast<device::cuda::Handle *>(handle_); | ||
| auto result = SoftmaxInfo::create(y, x, axis); | ||
| CHECK_RESULT(result); | ||
| CHECK_SAME_SHAPE(y->shape(), x->shape()); | ||
| CHECK_DTYPE(y->dtype(), x->dtype(), INFINI_DTYPE_F16, INFINI_DTYPE_F32, INFINI_DTYPE_BF16); | ||
| *desc_ptr = new Descriptor( | ||
| dtype, | ||
| result.take(), | ||
| 0, | ||
| new Opaque{handle->internal()}, | ||
| handle->device, handle->device_id); | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t Descriptor::calculate( | ||
| void *workspace, | ||
| size_t workspace_size, | ||
| void *y, | ||
| const void *x, | ||
| void *stream_) const { | ||
|
|
||
| switch (_dtype) { | ||
| case INFINI_DTYPE_F16: | ||
| return softmax_dispatch<half>(_info, y, x, stream_); | ||
| case INFINI_DTYPE_F32: | ||
| return softmax_dispatch<float>(_info, y, x, stream_); | ||
| case INFINI_DTYPE_BF16: | ||
| return softmax_dispatch<__nv_bfloat16>(_info, y, x, stream_); | ||
| default: | ||
| return INFINI_STATUS_BAD_TENSOR_DTYPE; | ||
| } | ||
| } | ||
| } // namespace op::softmax::cuda |
This file contains hidden or 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,8 @@ | ||
| #ifndef __SOFTMAX_CUDA_CUH__ | ||
| #define __SOFTMAX_CUDA_CUH__ | ||
|
|
||
| #include "../softmax.h" | ||
|
|
||
| DESCRIPTOR(cuda) | ||
|
|
||
| #endif // __SOFTMAX_CUDA_CUH__ |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.