-
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
Dev ivalue for cpp api #6890
Dev ivalue for cpp api #6890
Changes from 33 commits
2ce99e9
d2e0f7e
7bad9df
f7b9613
d4170af
028706b
0d2a89b
9017bce
169f635
98c959b
26a7900
d3fde21
9d51a99
cd838cc
b3edd69
98ae369
f59d4b4
eb6bb63
6856b95
bf3093c
ca06c75
7635975
b8caadd
b42ff88
d780d1b
753bee0
95e7dd8
6ba2a2f
7ac9701
5e3ee5f
1b74505
05bb3df
208187f
6ea031d
e32c922
5439783
14cc544
0c17f31
9e3a2da
3826231
4aa9b8e
bc52f5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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."; | ||
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 |
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; } | ||
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. 这些 explicit 是不是可以去掉,这样 Graph::Forward 的参数类型是 IValue 的话,用户仍然可以写 graph.Forward(tensor) 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. 那所有的构造函数都去掉explicit还是只去掉tensor和tensor_vector的? 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. 只去掉tensor和tensor_vector的吧 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. 好的 |
||
|
||
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(); | ||
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. bool IsX() const,另外这种特别简单的方法可以考虑在头文件定义,便于内联优化。 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. 好的 |
||
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() {} | ||
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. union 在析构的时候是不会主动析构 field 的,当构造了 Tensor 或者 vector 之后它们析构函数不会被调用,这样会造成内存泄露 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. union 中包含 non POD type 的时候处理所有权和生命周期要非常小心 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. 对 所以在IValue析构函数里面处理了union的析构 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. 哦哦,抱歉没看到。感觉构造/析构函数写在头文件好一些,方便内联优化。 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. 有道理 |
||
}; | ||
|
||
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_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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_; | ||
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. 感觉 operator= 之前不需要 reset,下同。reset 相当于 shared_ptr().swap(*this),operator= 相当于 shared_ptr(v).swap(*this),都会调临时对象的 dtor 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. 好的 |
||
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())); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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(); | ||||||
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.
Suggested change
然后可以删去 .cpp 内的 |
||||||
|
||||||
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; | ||||||
|
@@ -58,4 +67,4 @@ class Tensor final { | |||||
|
||||||
} // namespace oneflow_api | ||||||
|
||||||
#endif // !ONEFLOW_API_CPP_FRAMEWORK_TENSOR_H_ | ||||||
#endif // ONEFLOW_API_CPP_FRAMEWORK_TENSOR_H_ |
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.
错误信息是不是应该是 "is not an int"
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.
有道理
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.
int 应该是integer的简写