-
Notifications
You must be signed in to change notification settings - Fork 691
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
move python code to cpp: eye #7036
Merged
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8c97652
80% Sbp signature left to finish
doombeaker a78df26
refine functional_api.yaml
doombeaker d0f19cc
90% docstr left to update
doombeaker f03a33f
refine
doombeaker 9d2edda
add sbp check
doombeaker 4df3418
refine docs
doombeaker 423f455
Merge branch 'master' into py2cpp_eye_op
doombeaker 0ee52c5
auto format by CI
oneflow-ci-bot 3a72419
refine
doombeaker c8a36a0
Merge branch 'py2cpp_eye_op' of github.com:Oneflow-Inc/oneflow into p…
doombeaker 3ca3a5f
refine docstr
doombeaker 958a3b4
Merge branch 'master' into py2cpp_eye_op
doombeaker d48cde6
Merge branch 'master' into py2cpp_eye_op
oneflow-ci-bot 236f717
Merge branch 'master' into py2cpp_eye_op
doombeaker d3320af
Merge branch 'master' into py2cpp_eye_op
doombeaker 6b9ef97
Merge branch 'master' into py2cpp_eye_op
doombeaker 163d573
auto format by CI
oneflow-ci-bot 58a1627
Merge branch 'master' into py2cpp_eye_op
oneflow-ci-bot 1ff375a
Merge branch 'master' into py2cpp_eye_op
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
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,137 @@ | ||
/* | ||
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/just.h" | ||
#include "oneflow/core/common/maybe.h" | ||
#include "oneflow/core/common/scalar.h" | ||
#include "oneflow/core/common/throw.h" | ||
#include "oneflow/core/common/util.h" | ||
#include "oneflow/core/framework/attr_map.h" | ||
#include "oneflow/core/framework/nd_sbp.h" | ||
#include "oneflow/core/framework/op_builder.h" | ||
#include "oneflow/core/framework/op_expr.h" | ||
#include "oneflow/core/framework/op_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/framework/tensor.h" | ||
#include "oneflow/core/framework/tensor_tuple.h" | ||
#include "oneflow/core/functional/functional.h" | ||
#include "oneflow/core/functional/function_library.h" | ||
#include "oneflow/core/functional/functional_api.yaml.h" | ||
#include "oneflow/core/functional/impl/common.h" | ||
#include "oneflow/core/job/lazy_mode.h" | ||
#include "oneflow/core/job/sbp_parallel.h" | ||
#include "oneflow/api/common/device.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
namespace functional { | ||
|
||
namespace impl { | ||
|
||
class EyeDevcieFunctor { | ||
doombeaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
EyeDevcieFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } | ||
Maybe<Tensor> operator()(const Scalar& rows, const Optional<Scalar>& cols, | ||
const Symbol<DType>& dtype, const Optional<Symbol<Device>>& device, | ||
const bool& requires_grad) const { | ||
MutableAttrMap attrs; | ||
JUST(attrs.SetAttr<int64_t>("rows", JUST(rows.As<int64_t>()))); | ||
JUST(attrs.SetAttr<int64_t>("cols", JUST(cols.value_or(rows).As<int64_t>()))); | ||
JUST(attrs.SetAttr<DataType>("dtype", dtype->data_type())); | ||
OpExprInterpContext ctx(attrs); | ||
ctx.device = device; | ||
auto res = JUST(OpInterpUtil::Dispatch<Tensor>(*op_, {}, ctx)); | ||
JUST(res->set_requires_grad(requires_grad)); | ||
return res; | ||
doombeaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private: | ||
std::shared_ptr<OpExpr> op_; | ||
}; | ||
|
||
class EyeDeviceStrFunctor { | ||
public: | ||
Maybe<Tensor> operator()(const Scalar& rows, const Optional<Scalar>& cols, | ||
const Symbol<DType>& dtype, const std::string& device, | ||
const bool& requires_grad) const { | ||
const Symbol<Device>& dev = JUST(DeviceExportUtil::ParseAndNew(device)); | ||
return JUST(functional::Eye(rows, cols, dtype, dev, requires_grad)); | ||
} | ||
}; | ||
|
||
class ConsistentEyeSbpListFunctor { | ||
doombeaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
ConsistentEyeSbpListFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } | ||
Maybe<Tensor> operator()(const Scalar& rows, const Optional<Scalar>& cols, | ||
const Symbol<DType>& dtype, const bool& requires_grad, | ||
const Symbol<ParallelDesc>& placement, | ||
const std::vector<Symbol<cfg::SbpParallel>>& sbp_tuple) const { | ||
MutableAttrMap attrs; | ||
CHECK_EQ_OR_RETURN(sbp_tuple.size(), placement->hierarchy()->NumAxes()) | ||
<< "len(sbp) == len(placement.hierarchy) required, but " | ||
<< "len(sbp)==" << sbp_tuple.size() << ", " | ||
<< "len(placement.hierarchy)==" << placement->hierarchy()->NumAxes(); | ||
|
||
FOR_RANGE(int32_t, i, 0, sbp_tuple.size()) { | ||
CHECK_OR_RETURN(sbp_tuple.at(i)->has_broadcast_parallel()) | ||
<< "sbp of eye should be broadcast only"; | ||
} | ||
|
||
JUST(attrs.SetAttr<int64_t>("rows", JUST(rows.As<int64_t>()))); | ||
JUST(attrs.SetAttr<int64_t>("cols", JUST(cols.value_or(rows).As<int64_t>()))); | ||
JUST(attrs.SetAttr<DataType>("dtype", dtype->data_type())); | ||
if (LazyMode::is_enabled()) { | ||
std::vector<std::string> nd_sbp(sbp_tuple.size()); | ||
doombeaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
for (int i = 0; i < sbp_tuple.size(); ++i) { | ||
nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); | ||
} | ||
} | ||
JUST(attrs.SetAttr<std::vector<std::string>>("nd_sbp", nd_sbp)); | ||
} | ||
const auto& nd_sbp = JUST(GetNdSbp(sbp_tuple)); | ||
auto res = JUST( | ||
OpInterpUtil::Dispatch<Tensor>(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp))); | ||
JUST(res->set_requires_grad(requires_grad)); | ||
return res; | ||
doombeaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private: | ||
std::shared_ptr<OpExpr> op_; | ||
}; | ||
|
||
class ConsistentEyeSbpFunctor { | ||
public: | ||
Maybe<Tensor> operator()(const Scalar& rows, const Optional<Scalar>& cols, | ||
const Symbol<DType>& dtype, const bool& requires_grad, | ||
const Symbol<ParallelDesc>& placement, | ||
const Symbol<cfg::SbpParallel>& sbp) const { | ||
std::vector<Symbol<cfg::SbpParallel>> sbp_tuple{sbp}; | ||
return JUST(functional::Eye(rows, cols, dtype, requires_grad, placement, sbp_tuple)); | ||
} | ||
}; | ||
|
||
} // namespace impl | ||
|
||
using namespace impl; | ||
|
||
ONEFLOW_FUNCTION_LIBRARY(m) { | ||
m.add_functor<EyeDevcieFunctor, EyeDeviceStrFunctor, ConsistentEyeSbpListFunctor, | ||
ConsistentEyeSbpFunctor>("Eye"); | ||
}; | ||
|
||
} // namespace functional | ||
} // 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
This file was deleted.
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.
api 目录下的头文件只能被 api 目录下的文件 include 的
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.
这个是某些情况下编译不过吗?然后我本地以及CI没有遇到这个坑所以过了?
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.
主要是代码结构上的要求,oneflow api 可以依赖 oneflow 本体,oneflow 本体不应该依赖 oneflow api
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引入的问题,也是周泽楷在修吗,还是还没人修,没人修的话,我自己提个fix PR。
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.
嗯嗯对的 zekai 在修,在把 ParseAndNew 移动到 Device 类里