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

LazyInterpret build LocalTensor if input is local #5582

Merged
merged 3 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions oneflow/core/framework/op_interpreter/lazy_op_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ Maybe<void> LazyInterpreter::ApplyImpl(const FeedInputOpExpr& op_expr, const Ten
const auto& blob_attr = JUST(compatible_py::GetOpArgBlobAttribute(op_attr, obn));

CHECK_OR_RETURN(!outputs->at(0).get());
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /*is_lazy=*/true));
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /* is_lazy= */ true,
/* is_local= */ input_tensor->is_local()));
TensorNameScope::Global()->Record(outputs->at(0), GenLogicalBlobName(op_conf.name(), obn));
return Maybe<void>::Ok();
}
Expand Down Expand Up @@ -163,7 +164,8 @@ Maybe<void> LazyInterpreter::ApplyImpl(const FeedVariableOpExpr& op_expr, const
const auto& blob_attr = JUST(compatible_py::GetOpArgBlobAttribute(op_attr, obn));

CHECK_OR_RETURN(!outputs->at(0).get());
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /*is_lazy=*/true));
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /* is_lazy= */ true,
/* is_local */ input_tensor->is_local()));
// NOTE(chengcheng): Record variable op output LazyTenosr
TensorNameScope::Global()->Record(outputs->at(0), GenLogicalBlobName(op_conf.name(), obn));
// NOTE(chengcheng): Record EagerTensor as variable tensor name
Expand All @@ -178,8 +180,6 @@ Maybe<void> LazyInterpreter::ApplyImpl(const FetchOutputOpExpr& op_expr, const T
CHECK_EQ_OR_RETURN(op_expr.input_size(), 1);
const std::shared_ptr<Tensor>& input_tensor = inputs.at(0);
CHECK_OR_RETURN(input_tensor->is_lazy());
// NOTE(chengcheng): Lazy always consistent.
CHECK_OR_RETURN(input_tensor->is_consistent());
const std::string& input_lbn = TensorNameScope::Global()->Lookup(input_tensor);
CHECK_OR_RETURN(!input_lbn.empty()); // lbn must exist.

Expand Down Expand Up @@ -223,7 +223,8 @@ Maybe<void> LazyInterpreter::ApplyImpl(const FetchOutputOpExpr& op_expr, const T

CHECK_OR_RETURN(!outputs->at(0).get());
// TODO(chengcheng): Build EagerLocalTensor if parllel attr is this rank.
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /*is_lazy=*/false));
(*outputs)[0] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /* is_lazy= */ false,
/* is_local= */ input_tensor->is_local()));
return Maybe<void>::Ok();
}

Expand All @@ -248,10 +249,12 @@ Maybe<void> LazyInterpreter::ApplyImpl(const UserOpExpr& op_expr, const TensorTu
// if inputs size == 0, need handle in SourceUserOp impl.
CHECK_GE_OR_RETURN(inputs.size(), 1);
const std::string device_tag = GetDeviceTagOfTensor(inputs.at(0));
const bool is_local = inputs.at(0)->is_local();
op_conf->set_device_tag(device_tag);
for (int i = 0; i < inputs.size(); ++i) {
const auto& input_tensor = inputs.at(i);
CHECK_OR_RETURN(device_tag == GetDeviceTagOfTensor(input_tensor));
CHECK_EQ_OR_RETURN(is_local, input_tensor->is_local());
const std::string& ibn = op_expr.indexed_ibns().at(i);
const std::string& lbn = TensorNameScope::Global()->Lookup(inputs[i]);
if (lbn.empty()) {
Expand Down Expand Up @@ -300,7 +303,8 @@ Maybe<void> LazyInterpreter::ApplyImpl(const UserOpExpr& op_expr, const TensorTu
JUST(compatible_py::GetOpArgParallelAttribute(blob_parallel_desc_sym, op_attr, obn));
const auto& blob_attr = JUST(compatible_py::GetOpArgBlobAttribute(op_attr, obn));
if (!(outputs->at(i).get())) {
(*outputs)[i] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr, /*is_lazy=*/true));
(*outputs)[i] = JUST(OpInterpUtil::BuildTensor(blob_attr, parallel_attr,
/* is_lazy= */ true, is_local));
} else {
// TODO(chengcheng, hjchen2) Reset shape, dtype and so on for InplaceUserOp.
UNIMPLEMENTED();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ template<>

/* static */ Maybe<Tensor> OpInterpUtil::BuildTensor(
const std::shared_ptr<compatible_py::OpArgBlobAttribute>& blob_attr,
const std::shared_ptr<compatible_py::OpArgParallelAttribute>& parallel_attr,
const bool is_lazy) {
const std::shared_ptr<compatible_py::OpArgParallelAttribute>& parallel_attr, const bool is_lazy,
const bool is_local) {
const auto& dtype = DataType(blob_attr->get_dtype());
if (parallel_attr->is_mirrored()) {
if (is_local) {
const auto& device =
JUST(Device::MakeDeviceByParallelDesc(*parallel_attr->parallel_desc_symbol()));
const auto& tensor = JUST(MirroredTensor::MakeTensor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class OpInterpUtil {
static Maybe<Tensor> BuildTensor(
const std::shared_ptr<compatible_py::OpArgBlobAttribute>& blob_attr,
const std::shared_ptr<compatible_py::OpArgParallelAttribute>& parallel_attr,
const bool is_lazy);
const bool is_lazy, const bool is_local);
};

} // namespace one
Expand Down
4 changes: 2 additions & 2 deletions oneflow/python/framework/graph_build_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
@contextmanager
def graph_build_context(config_proto, session):
prev_scope = oneflow._oneflow_internal.GetCurrentScope()
device_tag_and_ids = placement_util.GetDefaultMachineDeviceIds(session.resource)
new_scope = scope_util.MakeInitialScope(
config_proto,
*device_tag_and_ids,
"cpu", # NOTE(chengcheng): graph init scope is useless, just set cpu 0:0 for test.
["0:0"],
None, # TODO(): set hierarchy from user graph config
False, # is_mirrored
)
Expand Down
2 changes: 1 addition & 1 deletion oneflow/python/test/graph/test_input_op_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_feed_input_tensor(test_case):
out_tensor = input_op.apply([x_tensor_in_c], attrs)[0]
test_case.assertEqual(out_tensor.shape, (1, 1, 10, 10))
test_case.assertTrue(out_tensor.is_lazy)
test_case.assertTrue(out_tensor.is_consistent)
test_case.assertTrue(out_tensor.is_local)


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions oneflow/python/test/graph/test_output_op_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ def test_fetch_output_tensor(test_case):
lazy_tensor = input_op.apply([x_tensor_in_c], attrs)[0]
test_case.assertEqual(lazy_tensor.shape, (1, 1, 10, 10))
test_case.assertTrue(lazy_tensor.is_lazy)
test_case.assertTrue(lazy_tensor.is_consistent)
test_case.assertTrue(lazy_tensor.is_local)

eager_tensor = output_op.apply([lazy_tensor], attrs)[0]
test_case.assertEqual(eager_tensor.shape, (1, 1, 10, 10))
test_case.assertTrue(not eager_tensor.is_lazy)
test_case.assertTrue(eager_tensor.is_consistent)
test_case.assertTrue(eager_tensor.is_local)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion oneflow/python/test/graph/test_variable_op_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_feed_var_tensor(test_case):
out_tensor = var_op.apply([x_tensor_in_c], attrs)[0]
test_case.assertEqual(out_tensor.shape, (1, 1, 10, 10))
test_case.assertTrue(out_tensor.is_lazy)
test_case.assertTrue(out_tensor.is_consistent)
test_case.assertTrue(out_tensor.is_local)


if __name__ == "__main__":
Expand Down