Skip to content
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

optimize reshape/slice/transpose functor #6956

Merged
merged 24 commits into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cbc5680
optimize reshape/slice/transpose functor
wyushun Dec 7, 2021
7da3940
update code according to reviewer's suggestion
wyushun Dec 7, 2021
16cd73e
judge negative dimension number besides -1
wyushun Dec 7, 2021
ba9a4ad
judge negative shape value in view::Reshape
wyushun Dec 8, 2021
8c2fce7
remove is_full_slice logic in SliceFunctor
wyushun Dec 8, 2021
8818d4c
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 8, 2021
dcd1ac7
update code according to yinggang's advice
wyushun Dec 8, 2021
b06f54d
Merge branch 'master' into wyushun_optimize_functor
oneflow-ci-bot Dec 8, 2021
bc279a5
move ordered permute judge to TransposeKernel
wyushun Dec 8, 2021
5bb1dc9
Merge branch 'wyushun_optimize_functor' of github.com:Oneflow-Inc/one…
wyushun Dec 8, 2021
7aaf94a
remove print sentence
wyushun Dec 9, 2021
6e11a1e
abstract IsOrderedPermute func
wyushun Dec 9, 2021
360ec25
support negative permute value in TransposeFunctor
wyushun Dec 9, 2021
e026434
delete tranpose_kernel optimization
wyushun Dec 9, 2021
eaa67dd
Revert "delete tranpose_kernel optimization"
wyushun Dec 9, 2021
d3c01d3
not return original tensor when reshape do nothing
wyushun Dec 9, 2021
3275f7e
simplify code
wyushun Dec 10, 2021
a4426ed
correct spell error
wyushun Dec 10, 2021
9154b8d
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 10, 2021
00ca04a
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 10, 2021
ec4fa8a
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 11, 2021
058eeb7
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 11, 2021
44d8e3d
Merge branch 'master' into wyushun_optimize_functor
wyushun Dec 11, 2021
9e00586
Merge branch 'master' into wyushun_optimize_functor
oneflow-ci-bot Dec 12, 2021
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
4 changes: 3 additions & 1 deletion oneflow/core/framework/tensor_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ Maybe<Tensor> Reshape(const std::shared_ptr<Tensor>& input, const Shape& shape)
int need_infer_axis = -1;
size_t count = 1;
for (int i = 0; i < shape.NumAxes(); ++i) {
if (shape.At(i) == -1) {
if (shape.At(i) < -1) {
return Error::RuntimeError() << "Invalid shape dimension " << shape.At(i);
} else if (shape.At(i) == -1) {
CHECK_EQ_OR_RETURN(need_infer_axis, -1)
<< "Shape " << shape.ToString() << " has more than 1 axis that needs to be infered.";
need_infer_axis = i;
Expand Down
4 changes: 3 additions & 1 deletion oneflow/core/functional/impl/array_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,9 @@ class ReshapeFunctor {
int need_infer_axis = -1;
wyushun marked this conversation as resolved.
Show resolved Hide resolved
size_t count = 1;
for (int i = 0; i < shape.NumAxes(); ++i) {
if (shape.At(i) == -1) {
if (shape.At(i) < -1) {
return Error::RuntimeError() << "Invalid shape dimension " << shape.At(i);
} else if (shape.At(i) == -1) {
CHECK_EQ_OR_RETURN(need_infer_axis, -1)
<< "Shape " << shape.ToString() << " has more than 1 axis that needs to be infered.";
need_infer_axis = i;
Expand Down
25 changes: 13 additions & 12 deletions oneflow/core/functional/impl/math_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,19 +575,20 @@ class TransposeFunctor {
Maybe<Tensor> operator()(const std::shared_ptr<one::Tensor>& input,
const std::vector<int32_t>& permute) const {
MutableAttrMap attrs;
CHECK_EQ_OR_RETURN(input->ndim(), permute.size()) << "number of dims don't match in permute";
JUST(attrs.SetAttr<std::vector<int32_t>>("perm", permute));
int32_t ndims = input->shape()->NumAxes();
for (int i = 0; i < permute.size(); i++) {
int32_t dim = permute.at(i);
if (dim < 0) { dim += ndims; }
CHECK_GE_OR_RETURN(dim, 0)
<< "IndexError: Dimension out of range (expected to be in range of [" << -ndims << ","
<< ndims << " ] but got " << ndims;
CHECK_LT_OR_RETURN(dim, ndims)
<< "IndexError: Dimension out of range (expected to be in range of [" << -ndims << ","
<< ndims << " ] but got " << ndims;
auto ndim = input->ndim();
CHECK_EQ_OR_RETURN(ndim, permute.size()) << "number of dims don't match in permute";

// handle negative permute value here, because of permute is const,
// so copy it to local var and do modification.
auto positive_perm = permute;
for (auto i = 0; i < positive_perm.size(); i++) {
if (positive_perm[i] < 0) { positive_perm[i] += ndim; }
CHECK_OR_RETURN(positive_perm[i] >= 0 && positive_perm[i] < ndim)
<< "IndexError: Dimension out of range (expected to be in range of [" << -ndim << ","
<< ndim << " ) but got " << positive_perm[i];
}

JUST(attrs.SetAttr<std::vector<int32_t>>("perm", positive_perm));
return OpInterpUtil::Dispatch<Tensor>(*op_, {input}, attrs);
}

Expand Down
23 changes: 21 additions & 2 deletions oneflow/user/kernels/transpose_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneflow/core/framework/framework.h"
#include "oneflow/core/kernel/kernel_util.h"
#include "oneflow/core/kernel/cuda_graph_support.h"
#include "oneflow/core/ep/include/primitive/permute.h"
namespace oneflow {

namespace user_op {

namespace {
bool IsIdentity(const std::vector<int32_t>& perm) {
for (auto i = 0; i < perm.size(); i++) {
if (perm[i] != i) { return false; }
}
return true;
}
} // namespace

template<typename Context>
std::unique_ptr<ep::primitive::Permute> NewPermutePrimitive(Context* ctx) {
const int64_t num_dims = ctx->TensorDesc4ArgNameAndIndex("output", 0)->shape().NumAxes();
Expand All @@ -46,9 +56,18 @@ class TransposeKernel final : public OpKernel, public user_op::CudaGraphSupport
const int64_t* src_dims = in_shape.ptr();

int64_t elem_cnt = tensor_out->shape().elem_cnt();

wyushun marked this conversation as resolved.
Show resolved Hide resolved
if (elem_cnt != 0) {
primitive->Launch(ctx->stream(), dtype, num_dims, src_dims, tensor_in->dptr(), perm.data(),
tensor_out->mut_dptr());
if (IsIdentity(perm)) {
// if permute vector is 0,1,...,n, do data copy directly
wyushun marked this conversation as resolved.
Show resolved Hide resolved
AutoMemcpy(ctx->stream(), tensor_out->mut_dptr(), tensor_in->dptr(),
elem_cnt * GetSizeOfDataType(dtype), tensor_out->mem_case(),
tensor_in->mem_case());
} else {
primitive->Launch(ctx->stream(), dtype, num_dims, src_dims, tensor_in->dptr(), perm.data(),
tensor_out->mut_dptr());
}

} else {
// For 0-d Tensor
return;
Expand Down