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

Dev ivalue for cpp api #6890

Merged
merged 42 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2ce99e9
add api tensor
mosout Nov 18, 2021
d2e0f7e
refine
mosout Nov 19, 2021
7bad9df
add nn.relu
mosout Nov 19, 2021
f7b9613
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 19, 2021
d4170af
refine
mosout Nov 19, 2021
028706b
clean shape & refine relu test
mosout Nov 19, 2021
0d2a89b
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 19, 2021
9017bce
support void* for from_blob
mosout Nov 24, 2021
169f635
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 24, 2021
98c959b
add multithreading relu test
mosout Nov 24, 2021
26a7900
refine test
mosout Nov 24, 2021
d3fde21
refine
mosout Nov 26, 2021
9d51a99
refine
mosout Nov 26, 2021
cd838cc
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 26, 2021
b3edd69
add comment for __internal_tensor()
mosout Nov 26, 2021
98ae369
convert to copy_util
mosout Nov 26, 2021
f59d4b4
reformat
mosout Nov 26, 2021
eb6bb63
refine
mosout Nov 26, 2021
6856b95
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 26, 2021
bf3093c
Merge remote-tracking branch 'upstream/master' into dev_api_tensor
mosout Nov 29, 2021
ca06c75
add ivalue
mosout Nov 29, 2021
7635975
refine directory structure
mosout Nov 30, 2021
b8caadd
Merge remote-tracking branch 'origin/dev_api_tensor' into dev_ivalue
mosout Nov 30, 2021
b42ff88
refine cpp api test
mosout Nov 30, 2021
d780d1b
Merge remote-tracking branch 'origin/dev_api_tensor' into dev_ivalue
mosout Nov 30, 2021
753bee0
refine test
mosout Nov 30, 2021
95e7dd8
Merge remote-tracking branch 'origin/dev_api_tensor' into dev_ivalue
mosout Nov 30, 2021
6ba2a2f
add ivalue
mosout Dec 2, 2021
7ac9701
Merge remote-tracking branch 'upstream/master' into dev_ivalue
mosout Dec 2, 2021
5e3ee5f
refine ivalue
mosout Dec 3, 2021
1b74505
Merge remote-tracking branch 'upstream/master' into dev_ivalue
mosout Dec 3, 2021
05bb3df
refine ivalue
mosout Dec 11, 2021
208187f
Merge remote-tracking branch 'upstream/master' into dev_ivalue
mosout Dec 11, 2021
6ea031d
refine
mosout Dec 11, 2021
e32c922
Merge branch 'master' into dev_ivalue
oneflow-ci-bot Dec 11, 2021
5439783
refine
mosout Dec 11, 2021
14cc544
Merge remote-tracking branch 'origin/dev_ivalue' into dev_ivalue
mosout Dec 11, 2021
0c17f31
refine
mosout Dec 11, 2021
9e3a2da
refine
mosout Dec 11, 2021
3826231
Merge branch 'master' into dev_ivalue
oneflow-ci-bot Dec 11, 2021
4aa9b8e
Merge branch 'master' into dev_ivalue
oneflow-ci-bot Dec 11, 2021
bc52f5c
Merge branch 'master' into dev_ivalue
oneflow-ci-bot Dec 11, 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
3 changes: 2 additions & 1 deletion oneflow/api/cpp/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ limitations under the License.
#include "framework/shape.h"
#include "framework/dtype.h"
#include "framework/tensor.h"
#include "framework/ivalue.h"

#endif // !ONEFLOW_API_CPP_FRAMEWORK_H_
#endif // ONEFLOW_API_CPP_FRAMEWORK_H_
2 changes: 1 addition & 1 deletion oneflow/api/cpp/framework/dtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ enum class DType {

} // namespace oneflow_api

#endif // !ONEFLOW_API_CPP_FRAMEWORK_DTYPE_H_
#endif // ONEFLOW_API_CPP_FRAMEWORK_DTYPE_H_
65 changes: 65 additions & 0 deletions oneflow/api/cpp/framework/ivalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
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/api/cpp/framework/ivalue.h"
#include <glog/logging.h>

namespace oneflow_api {

namespace of = oneflow;

std::ostream& operator<<(std::ostream& os, const IValue::Tag& tag) {
os << static_cast<int>(tag);
return os;
}

bool IValue::IsNone() { return tag_ == Tag::kNone; }

bool IValue::IsInt() { return tag_ == Tag::kInt; }

bool IValue::IsDouble() { return tag_ == Tag::kDouble; }

bool IValue::IsBool() { return tag_ == Tag::kBool; }

bool IValue::IsTensor() { return tag_ == Tag::kTensor; }

bool IValue::IsTensorVector() { return tag_ == Tag::kTensorVector; }

const int64_t IValue::ToInt() const {
CHECK_EQ(tag_, Tag::kInt) << "Current value is not int.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

错误信息是不是应该是 "is not an int"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int 应该是integer的简写

return payload_.i.v_int;
}

const double IValue::ToDouble() const {
CHECK_EQ(tag_, Tag::kDouble) << "Current value is not double.";
return payload_.i.v_double;
}

const bool IValue::ToBool() const {
CHECK_EQ(tag_, Tag::kBool) << "Current value is not bool.";
return payload_.i.v_bool;
}

const Tensor& IValue::ToTensor() const {
CHECK_EQ(tag_, Tag::kTensor) << "Current value is not tensor.";
return payload_.v_tensor;
}

const std::vector<Tensor>& IValue::ToTensorVector() const {
CHECK_EQ(tag_, Tag::kTensorVector) << "Current value is not vector of tensor.";
return payload_.v_tensor_vector;
}

} // namespace oneflow_api
144 changes: 144 additions & 0 deletions oneflow/api/cpp/framework/ivalue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
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.
*/
#ifndef ONEFLOW_API_CPP_FRAMEWORK_IVALUE_H_
#define ONEFLOW_API_CPP_FRAMEWORK_IVALUE_H_

#include <cstdint>
#include <memory>
#include <vector>
#include "tensor.h"

namespace oneflow_api {

class IValue {
public:
IValue() : tag_(IValue::Tag::kNone) {}
explicit IValue(int value) : tag_(IValue::Tag::kInt) { payload_.i.v_int = value; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这些 explicit 是不是可以去掉,这样 Graph::Forward 的参数类型是 IValue 的话,用户仍然可以写 graph.Forward(tensor)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那所有的构造函数都去掉explicit还是只去掉tensor和tensor_vector的?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只去掉tensor和tensor_vector的吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的


explicit IValue(int64_t value) : tag_(IValue::Tag::kInt) { payload_.i.v_int = value; }

explicit IValue(double value) : tag_(IValue::Tag::kDouble) { payload_.i.v_double = value; }

explicit IValue(bool value) : tag_(IValue::Tag::kBool) { payload_.i.v_bool = value; }

explicit IValue(const Tensor& value) : tag_(IValue::Tag::kTensor) {
new (&payload_.v_tensor) Tensor(value);
}

explicit IValue(Tensor&& value) : tag_(IValue::Tag::kTensor) {
new (&payload_.v_tensor) Tensor(std::move(value));
}

explicit IValue(const std::vector<Tensor>& value) : tag_(IValue::Tag::kTensorVector) {
new (&payload_.v_tensor_vector) std::vector<Tensor>(value);
}

explicit IValue(std::vector<Tensor>&& value) : tag_(IValue::Tag::kTensorVector) {
new (&payload_.v_tensor_vector) std::vector<Tensor>(std::move(value));
}

IValue(const IValue& value) : tag_(value.tag_) {
if (IsTensor()) {
new (&payload_.v_tensor) Tensor(value.payload_.v_tensor);
} else if (IsTensorVector()) {
new (&payload_.v_tensor_vector) std::vector<Tensor>(value.payload_.v_tensor_vector);
} else {
payload_.i = value.payload_.i;
}
}

IValue(IValue&& value) noexcept : tag_(value.tag_) { MoveFrom(std::move(value)); }

IValue& operator=(const IValue& value) {
if (&value == this) { return *this; }
this->tag_ = value.tag_;
*this = IValue(value);
return *this;
}

IValue& operator=(IValue&& value) noexcept {
if (&value == this) { return *this; }
Destory();
this->tag_ = value.tag_;
MoveFrom(std::move(value));
return *this;
}

~IValue() { Destory(); }

bool IsNone();
Copy link
Contributor

@PragmaTwice PragmaTwice Dec 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool IsX() const,另外这种特别简单的方法可以考虑在头文件定义,便于内联优化。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

bool IsInt();
bool IsDouble();
bool IsBool();
bool IsTensor();
bool IsTensorVector();

const int64_t ToInt() const;
const double ToDouble() const;
const bool ToBool() const;
const Tensor& ToTensor() const;
const std::vector<Tensor>& ToTensorVector() const;

private:
enum class Tag { kNone = 0, kInt = 1, kDouble = 2, kBool = 3, kTensor = 4, kTensorVector = 5 };
friend std::ostream& operator<<(std::ostream&, const Tag&);

union Payload { // NOLINT
union InternalPayload {
InternalPayload() : v_int(0) {}

int64_t v_int;
double v_double;
bool v_bool;
} i;

Tensor v_tensor;
std::vector<Tensor> v_tensor_vector;

Payload() : i() {}
~Payload() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

union 在析构的时候是不会主动析构 field 的,当构造了 Tensor 或者 vector 之后它们析构函数不会被调用,这样会造成内存泄露

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

union 中包含 non POD type 的时候处理所有权和生命周期要非常小心

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对 所以在IValue析构函数里面处理了union的析构

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦哦,抱歉没看到。感觉构造/析构函数写在头文件好一些,方便内联优化。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有道理

};

Payload payload_;
Tag tag_;

inline void Destory() {
if (IsTensor()) { payload_.v_tensor.~Tensor(); }
if (IsTensorVector()) { payload_.v_tensor_vector.~vector(); }
}

inline void MoveFrom(IValue&& value) {
if (IsTensor()) {
new (&payload_.v_tensor) Tensor(std::move(value.payload_.v_tensor));
} else if (IsTensorVector()) {
new (&payload_.v_tensor_vector)
std::vector<Tensor>(std::move(value.payload_.v_tensor_vector));
} else {
payload_.i = value.payload_.i;
}
value.ClearToNone();
}

inline void ClearToNone() {
Destory();
payload_.i.v_int = 0;
tag_ = Tag::kNone;
}
};

} // namespace oneflow_api

#endif // ONEFLOW_API_CPP_FRAMEWORK_IVALUE_H_
5 changes: 5 additions & 0 deletions oneflow/api/cpp/framework/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ int64_t Shape::Count(int64_t begin_axis, int64_t end_axis) const {

int64_t Shape::Count(int64_t begin_axis) const { return shape_->Count(begin_axis); }

std::ostream& operator<<(std::ostream& os, const Shape& shape) {
os << shape.shape_->DebugStr();
return os;
}

} // namespace oneflow_api
4 changes: 3 additions & 1 deletion oneflow/api/cpp/framework/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class Shape final {

private:
std::shared_ptr<oneflow::Shape> shape_ = nullptr;

friend std::ostream& operator<<(std::ostream&, const Shape&);
};
} // namespace oneflow_api

#endif // !ONEFLOW_API_CPP_FRAMEWORK_SHAPE_H_
#endif // ONEFLOW_API_CPP_FRAMEWORK_SHAPE_H_
17 changes: 16 additions & 1 deletion oneflow/api/cpp/framework/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
#include "oneflow/api/cpp/framework/tensor.h"
#include <memory>
#include "oneflow/api/cpp/framework/device.h"
#include "oneflow/api/cpp/framework/dtype.h"
#include "oneflow/api/cpp/framework/shape.h"
Expand Down Expand Up @@ -42,6 +41,22 @@ Tensor::Tensor(const Shape& shape, const Device& device, const DType& dtype) {
}
Tensor::Tensor(const std::shared_ptr<oneflow::one::Tensor>& tensor) : tensor_(tensor) {}

Tensor::Tensor(const Tensor& tensor) : tensor_(tensor.tensor_) {}
Tensor::Tensor(Tensor&& tensor) noexcept : tensor_(std::move(tensor.tensor_)) {}

Tensor::~Tensor() {}

Tensor& Tensor::operator=(const Tensor& tensor) {
tensor_.reset();
tensor_ = tensor.tensor_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉 operator= 之前不需要 reset,下同。reset 相当于 shared_ptr().swap(*this),operator= 相当于 shared_ptr(v).swap(*this),都会调临时对象的 dtor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

return *this;
}
Tensor& Tensor::operator=(Tensor&& tensor) noexcept {
tensor_.reset();
tensor_ = std::move(tensor.tensor_);
return *this;
}

const Shape Tensor::shape() const {
const auto shape_ = tensor_->shape();
return Shape(std::vector<int64_t>(shape_->dim_vec().begin(), shape_->dim_vec().end()));
Expand Down
11 changes: 10 additions & 1 deletion oneflow/api/cpp/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class Tensor final {
explicit Tensor(const Shape& shape = Shape(), const Device& device = Device("cpu"),
const DType& dtype = DType::kFloat);
explicit Tensor(const std::shared_ptr<oneflow::one::Tensor>& tensor);

Tensor(const Tensor& tensor);
Tensor(Tensor&& tensor) noexcept;

~Tensor();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
~Tensor();
~Tensor() = default;

然后可以删去 .cpp 内的 ~Tensor()


Tensor& operator=(const Tensor& tensor);
Tensor& operator=(Tensor&& tensor) noexcept;

[[nodiscard]] const Shape shape() const;
[[nodiscard]] const Device device() const;
[[nodiscard]] const DType dtype() const;
Expand All @@ -58,4 +67,4 @@ class Tensor final {

} // namespace oneflow_api

#endif // !ONEFLOW_API_CPP_FRAMEWORK_TENSOR_H_
#endif // ONEFLOW_API_CPP_FRAMEWORK_TENSOR_H_
2 changes: 1 addition & 1 deletion oneflow/api/cpp/nn.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ limitations under the License.

#include "nn/functional/activation.h"

#endif // !ONEFLOW_API_CPP_NN_H_
#endif // ONEFLOW_API_CPP_NN_H_
2 changes: 1 addition & 1 deletion oneflow/api/cpp/nn/functional/activation.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ Tensor relu(const Tensor& tensor);

} // namespace oneflow_api

#endif // !ONEFLOW_API_CPP_NN_FUNCTIONAL_ACTIVATION_H_
#endif // ONEFLOW_API_CPP_NN_FUNCTIONAL_ACTIVATION_H_
1 change: 0 additions & 1 deletion oneflow/api/cpp/tests/api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License.
*/

#include "oneflow/api/cpp/tests/api_test.h"
#include <cstdint>
#include <random>

namespace oneflow_api {
Expand Down
Loading