This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Softmax optimization for GPU #15545
Merged
Merged
Softmax optimization for GPU #15545
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
672af41
Softmax optimization
ptrendx c54736c
Fix lint
ptrendx da75ec1
Unifying softmax with length with regular softmax
ptrendx 40c0d00
Fix lint
ptrendx 9c33c4b
Fixes from review
ptrendx 99793ab
Making less templated kernels
ptrendx fea584d
Unifying softmaxgrad and softmaxwithlengthgrad
ptrendx 77d52fd
Better gradient of softmax
ptrendx 7638340
Dividing softmax.cc into multiple files
ptrendx 4d7462d
Merge branch 'upstream' into pr_softmax
ptrendx b7670c2
Fix
ptrendx 9a406d1
Trigger CI
ptrendx 46175c0
Moving get_rows_per_block to common place
ptrendx 9340a33
Fix
ptrendx c020cfd
Fix lint
ptrendx 61a2aad
Actually fix lint
ptrendx 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 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,90 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file cuda_utils.cc | ||
* \brief Common CUDA utilities. | ||
*/ | ||
|
||
#include <mxnet/base.h> | ||
#include <mshadow/base.h> | ||
|
||
#include <algorithm> | ||
|
||
#include "cuda_utils.h" | ||
|
||
#if MXNET_USE_CUDA | ||
|
||
namespace mxnet { | ||
namespace common { | ||
namespace cuda { | ||
|
||
namespace { | ||
bool IsPower2(size_t N) { | ||
return ((N & (N - 1)) == 0) && N != 0; | ||
} | ||
|
||
size_t RoundToPower2(size_t N) { | ||
size_t ret = 1; | ||
size_t copyN = N; | ||
while (N >= 2) { | ||
ret *= 2; | ||
N /= 2; | ||
} | ||
if (ret < copyN) { | ||
ret *= 2; | ||
} | ||
return ret; | ||
} | ||
} // namespace | ||
|
||
int get_load_type(size_t N) { | ||
using namespace mshadow; | ||
if (N % 8 == 0) { | ||
return kFloat64; | ||
} else if (N % 4 == 0) { | ||
return kFloat32; | ||
} else if (N % 2 == 0) { | ||
return kFloat16; | ||
} else { | ||
return kUint8; | ||
} | ||
} | ||
|
||
int get_rows_per_block(size_t row_size, int num_threads_per_block) { | ||
const int warp_size = 32; | ||
CHECK(IsPower2(num_threads_per_block)) | ||
<< "Number of threads in a block must be power of 2 to use get_rows_per_block function"; | ||
// How many read instructions should 1 thread at least do | ||
const int read_instructions = 2; | ||
const int desired_num_threads_per_row = (row_size + read_instructions - 1) / read_instructions; | ||
int desired_num_warps_per_row = (desired_num_threads_per_row + warp_size - 1) / warp_size; | ||
int actual_num_warps_per_row = std::min(desired_num_warps_per_row, | ||
num_threads_per_block / warp_size); | ||
// actual number of warps needs to be power of 2 | ||
actual_num_warps_per_row = RoundToPower2(desired_num_warps_per_row); | ||
return num_threads_per_block / (warp_size * actual_num_warps_per_row); | ||
} | ||
|
||
} // namespace cuda | ||
} // namespace common | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_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
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,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file log_softmax.cc | ||
* \brief CPU Implementation of log_softmax | ||
*/ | ||
#include "./softmax-inl.h" | ||
#include "../tensor/elemwise_unary_op.h" | ||
#include "../tensor/elemwise_binary_op.h" | ||
#include "../operator_common.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
NNVM_REGISTER_OP(log_softmax) | ||
.add_alias("_npx_log_softmax") | ||
.describe(R"code(Computes the log softmax of the input. | ||
This is equivalent to computing softmax followed by log. | ||
|
||
Examples:: | ||
|
||
>>> x = mx.nd.array([1, 2, .1]) | ||
>>> mx.nd.log_softmax(x).asnumpy() | ||
array([-1.41702998, -0.41702995, -2.31702995], dtype=float32) | ||
|
||
>>> x = mx.nd.array( [[1, 2, .1],[.1, 2, 1]] ) | ||
>>> mx.nd.log_softmax(x, axis=0).asnumpy() | ||
array([[-0.34115392, -0.69314718, -1.24115396], | ||
[-1.24115396, -0.69314718, -0.34115392]], dtype=float32) | ||
|
||
|
||
)code") | ||
.set_attr_parser(ParamParser<SoftmaxParam>) | ||
.set_attr<FCompute>("FCompute<cpu>", SoftmaxCompute<cpu, mxnet_op::log_softmax_fwd>) | ||
.set_attr<nnvm::FGradient>("FGradient", SoftmaxFGradient{"_backward_log_softmax"}) | ||
.set_attr<nnvm::FInferType>("FInferType", SoftmaxOpType) | ||
.set_num_inputs(1) | ||
.set_num_outputs(1) | ||
.set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<1, 1>) | ||
.set_attr<nnvm::FInplaceOption>("FInplaceOption", | ||
[](const NodeAttrs& attrs){ | ||
return std::vector<std::pair<int, int> >{{0, 0}}; | ||
}) | ||
.add_argument("data", "NDArray-or-Symbol", "The input array.") | ||
.add_arguments(SoftmaxParam::__FIELDS__()); | ||
|
||
NNVM_REGISTER_OP(_backward_log_softmax) | ||
.set_num_inputs(SoftmaxGradOpNumInputs) | ||
.set_num_outputs(1) | ||
.set_attr<nnvm::FListInputNames>("FListInputNames", SoftmaxGradOpInputNames) | ||
.set_attr<mxnet::FInferShape>("FInferShape", SoftmaxGradOpShape) | ||
.set_attr<nnvm::FInferType>("FInferType", SoftmaxGradOpType) | ||
.set_attr<nnvm::FInplaceOption>("FInplaceOption", SoftmaxGradOpInplaceOption) | ||
.add_argument("args", "NDArray-or-Symbol[]", "Positional input arguments") | ||
.set_attr_parser(ParamParser<SoftmaxParam>) | ||
.set_attr<FCompute>("FCompute<cpu>", SoftmaxGradCompute<cpu, mshadow_op::left, | ||
mxnet_op::log_softmax_bwd>); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
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,39 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* Copyright (c) 2017 by Contributors | ||
* \file log_softmax.cu | ||
* \brief GPU Implementation of log_softmax | ||
*/ | ||
#include "./softmax-inl.h" | ||
#include "../tensor/elemwise_unary_op.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
NNVM_REGISTER_OP(log_softmax) | ||
.set_attr<FCompute>("FCompute<gpu>", SoftmaxCompute<gpu, mxnet_op::log_softmax_fwd>); | ||
|
||
NNVM_REGISTER_OP(_backward_log_softmax) | ||
.set_attr<FCompute>("FCompute<gpu>", SoftmaxGradCompute<gpu, mshadow_op::left, | ||
mxnet_op::log_softmax_bwd>); | ||
|
||
} // namespace op | ||
} // namespace mxnet |
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.