-
Notifications
You must be signed in to change notification settings - Fork 690
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
Add Diagonal Op #6016
Merged
Merged
Add Diagonal Op #6016
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
822c56c
format complete
lcylcy 866d7e4
python to cpp
lcylcy 964eb63
py2cpp error
lcylcy 04df0a6
rm
lcylcy db00834
Merge branch 'master' into lcy_diagonal
lcylcy 8a0841f
Merge branch 'master' into lcy_diagonal
lcylcy dbc6725
auto format by CI
oneflow-ci-bot d341d6d
revise
lcylcy 60ccb10
auto format by CI
oneflow-ci-bot 530c498
Merge branch 'master' into lcy_diagonal
lcylcy ea5fad2
license
lcylcy baa569b
docstring
lcylcy 825031e
docstring
lcylcy 77de7cc
tensor
lcylcy 55f52f7
tensor attribute
lcylcy eeb0f31
auto format by CI
oneflow-ci-bot d254b10
docstring
lcylcy 0216b69
dtype
lcylcy a3cf315
revise
lcylcy 0f8c35e
test
lcylcy ada7c45
revise
lcylcy 1cd6b76
revise
lcylcy 69f875d
rename
lcylcy 6d30743
half
lcylcy 7296cd5
docs
lcylcy 93efdee
doc,test
lcylcy 2d45d8b
test times
lcylcy caa59fb
Merge branch 'master' into lcy_diagonal
lcylcy 4393edd
revise
lcylcy 4567d99
format
lcylcy 83e18bc
Merge branch 'master' into lcy_diagonal
lcylcy 825866d
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 77a9865
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 6fc43f3
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 6290d1b
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot a202b1d
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 8cf6e95
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot c857f7f
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 5f30b8f
Merge branch 'master' into lcy_diagonal
oneflow-ci-bot 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ oneflow | |
cos, | ||
cosh, | ||
diag, | ||
diagonal, | ||
movedim, | ||
div, | ||
dot, | ||
|
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 |
---|---|---|
|
@@ -42,6 +42,7 @@ OneFlow Tensor Class | |
detach, | ||
device, | ||
diag, | ||
diagonal, | ||
dim, | ||
div, | ||
double, | ||
|
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,72 @@ | ||
/* | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
|
||
Licensed 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. | ||
*/ | ||
#include "oneflow/core/framework/attr_map.h" | ||
#include "oneflow/core/framework/op_expr_grad_function.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct DiagonalInterpState : public AutoGradCaptureState { | ||
bool requires_grad = false; | ||
int32_t offset = 0; | ||
}; | ||
|
||
class Diagonal : public OpExprGradFunction<DiagonalInterpState> { | ||
public: | ||
Maybe<void> Init(const OpExpr& op) override; | ||
Maybe<void> Capture(DiagonalInterpState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override; | ||
Maybe<void> Apply(const DiagonalInterpState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override; | ||
|
||
private: | ||
AttrMap base_attrs_; | ||
}; | ||
|
||
Maybe<void> Diagonal::Init(const OpExpr& op) { | ||
const UserOpExpr* fw_op_expr = dynamic_cast<const UserOpExpr*>(&op); | ||
CHECK_NOTNULL_OR_RETURN(fw_op_expr); | ||
base_attrs_ = MakeAttrMapFromUserOpConf(fw_op_expr->proto()); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Diagonal::Capture(DiagonalInterpState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const { | ||
CHECK_EQ_OR_RETURN(outputs.size(), 1); | ||
ctx->requires_grad = inputs.at(0)->requires_grad(); | ||
if (!ctx->requires_grad) { return Maybe<void>::Ok(); } | ||
ComposedAttrMap composed_attrs(attrs, base_attrs_); | ||
ctx->offset = JUST(composed_attrs.GetAttr<int32_t>("offset")); | ||
ctx->SaveTensorForBackward(inputs.at(0)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Diagonal::Apply(const DiagonalInterpState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const { | ||
CHECK_EQ_OR_RETURN(out_grads.size(), 1); | ||
in_grads->resize(2); | ||
if (ctx->requires_grad) { | ||
const auto& x = ctx->SavedTensors().at(0); | ||
in_grads->at(0) = JUST(functional::DiagonalGrad(out_grads.at(0), x, ctx->offset)); | ||
} | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("diagonal", Diagonal); | ||
|
||
} // namespace one | ||
} // namespace oneflow | ||
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
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,139 @@ | ||
/* | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
|
||
Licensed 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. | ||
*/ | ||
|
||
#include "oneflow/core/common/util.h" | ||
#include "oneflow/core/framework/framework.h" | ||
#include "oneflow/core/kernel/new_kernel_util.h" | ||
#include "oneflow/core/kernel/kernel_util.h" | ||
#include "oneflow/core/ep/cuda/cuda_stream.h" | ||
|
||
|
||
namespace oneflow { | ||
namespace { | ||
|
||
template<typename T> | ||
struct DiagonalFunctor final { | ||
void operator()(ep::Stream* stream, T* out_buf, const T* in_buf, int32_t size, int32_t dim1, | ||
int32_t dim2) { | ||
int32_t offset_index = (dim1 + 1) * dim2; | ||
FOR_RANGE(int32_t, index, 0, size * dim2) { | ||
int32_t i = index / dim2; | ||
BBuf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int32_t j = index - i * dim2; | ||
out_buf[j * size + i] = in_buf[i * offset_index + j]; | ||
} | ||
} | ||
}; | ||
|
||
template<typename T> | ||
struct DiagonalGradFunctor final { | ||
void operator()(ep::Stream* stream, T* dx_buf, const T* dy_buf, int32_t size, int32_t dim1, | ||
int32_t dim2) { | ||
int32_t offset_index = (dim1 + 1) * dim2; | ||
FOR_RANGE(int32_t, index, 0, size * dim2) { | ||
BBuf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int32_t i = index / dim2; | ||
int32_t j = index - i * dim2; | ||
dx_buf[i * offset_index + j] = dy_buf[j * size + i]; | ||
} | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
template<typename T> | ||
class CpuDiagonalKernel final : public user_op::OpKernel { | ||
public: | ||
CpuDiagonalKernel() = default; | ||
~CpuDiagonalKernel() = default; | ||
|
||
private: | ||
void Compute(user_op::KernelComputeContext* ctx) const override { | ||
const int32_t offset = ctx->Attr<int32_t>("offset"); | ||
const user_op::Tensor* in = ctx->Tensor4ArgNameAndIndex("in", 0); | ||
user_op::Tensor* out = ctx->Tensor4ArgNameAndIndex("out", 0); | ||
const ShapeView& out_shape = out->shape(); | ||
const ShapeView& in_shape = in->shape(); | ||
const T* in_buf = in->dptr<T>(); | ||
T* out_buf = out->mut_dptr<T>(); | ||
|
||
int32_t size = out_shape.At(out_shape.NumAxes() - 1); | ||
int32_t dim1 = in_shape.At(1); | ||
int32_t dim2 = 0; | ||
if (in_shape.NumAxes() <= 2) { | ||
dim2 = 1; | ||
} else { | ||
dim2 = in_shape.Count(2, in_shape.NumAxes()); | ||
} | ||
|
||
int32_t offset_in_bufer = (offset >= 0 ? offset * dim2 : -offset * dim1 * dim2); | ||
in_buf += offset_in_bufer; | ||
DiagonalFunctor<T>()(ctx->stream(), out_buf, in_buf, size, dim1, dim2); | ||
} | ||
bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } | ||
}; | ||
|
||
template<typename T> | ||
class CpuDiagonalBackwardKernel final : public user_op::OpKernel { | ||
public: | ||
CpuDiagonalBackwardKernel() = default; | ||
~CpuDiagonalBackwardKernel() = default; | ||
|
||
private: | ||
void Compute(user_op::KernelComputeContext* ctx) const override { | ||
const user_op::Tensor* dy = ctx->Tensor4ArgNameAndIndex("dy", 0); | ||
user_op::Tensor* dx = ctx->Tensor4ArgNameAndIndex("dx", 0); | ||
int32_t offset = ctx->Attr<int32_t>("offset"); | ||
const ShapeView& dx_shape = dx->shape(); | ||
const ShapeView& dy_shape = dy->shape(); | ||
T* dx_buf = dx->mut_dptr<T>(); | ||
const T* dy_buf = dy->dptr<T>(); | ||
|
||
Memset<DeviceType::kCPU>(ctx->stream(), dx->mut_dptr<T>(), 0, dx_shape.elem_cnt() * sizeof(T)); | ||
|
||
int32_t dim1 = dx_shape.At(1); | ||
int32_t dim2 = 0; | ||
if (dx_shape.NumAxes() <= 2) { | ||
dim2 = 1; | ||
} else { | ||
dim2 = dx_shape.Count(2, dx_shape.NumAxes()); | ||
} | ||
int32_t size = dy_shape.At(dy_shape.NumAxes() - 1); | ||
int32_t offset_in_bufer = (offset >= 0 ? offset * dim2 : -offset * dim1 * dim2); | ||
dx_buf += offset_in_bufer; | ||
|
||
DiagonalGradFunctor<T>()(ctx->stream(), dx_buf, dy_buf, size, dim1, dim2); | ||
} | ||
bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } | ||
}; | ||
|
||
#define REGISTER_DIAGONAL_KERNELS(dtype) \ | ||
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. 记得 |
||
REGISTER_USER_KERNEL("diagonal") \ | ||
.SetCreateFn<CpuDiagonalKernel<dtype>>() \ | ||
.SetIsMatchedHob((user_op::HobDeviceType() == DeviceType::kCPU) \ | ||
&& (user_op::HobDataType("in", 0) == GetDataType<dtype>::value)); \ | ||
REGISTER_USER_KERNEL("diagonal_grad") \ | ||
.SetCreateFn<CpuDiagonalBackwardKernel<dtype>>() \ | ||
.SetIsMatchedHob((user_op::HobDeviceType() == DeviceType::kCPU) \ | ||
&& (user_op::HobDataType("in", 0) == GetDataType<dtype>::value)); | ||
|
||
REGISTER_DIAGONAL_KERNELS(float); | ||
REGISTER_DIAGONAL_KERNELS(double); | ||
REGISTER_DIAGONAL_KERNELS(int8_t); | ||
REGISTER_DIAGONAL_KERNELS(int32_t); | ||
REGISTER_DIAGONAL_KERNELS(int64_t); | ||
|
||
#undef REGISTER_DIAGONAL_KERNELS | ||
|
||
} // namespace oneflow | ||
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. 文件末换行 |
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.
文件末换行