Skip to content

Commit

Permalink
Dev ivalue for cpp api (#6890)
Browse files Browse the repository at this point in the history
* add api tensor

* refine

* add nn.relu

* refine

* clean shape & refine relu test

* support void* for from_blob

* add multithreading relu test

* refine test

* refine

* refine

* add comment for __internal_tensor()

* convert to copy_util

* reformat

* refine

* add ivalue

* refine directory structure

* refine cpp api test

* refine test

* add ivalue

* refine ivalue

* refine ivalue

* refine

* refine

* refine

* refine

Co-authored-by: oneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com>
  • Loading branch information
mosout and oneflow-ci-bot authored Dec 11, 2021
1 parent 55bb33d commit 4b5eafb
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 14 deletions.
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_
53 changes: 53 additions & 0 deletions oneflow/api/cpp/framework/ivalue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
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;
}

int64_t IValue::ToInt() const {
CHECK_EQ(tag_, Tag::kInt) << "Current value is not an int.";
return payload_.i.v_int;
}

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

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

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

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

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

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; }

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

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

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

IValue(std::vector<Tensor>&& value) : tag_(IValue::Tag::kTensorVector) { // NOLINT
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() const { return tag_ == Tag::kNone; }

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

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

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

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

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

int64_t ToInt() const;
double ToDouble() 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() {}
};

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_
21 changes: 17 additions & 4 deletions 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,17 +41,31 @@ Tensor::Tensor(const Shape& shape, const Device& device, const DType& dtype) {
}
Tensor::Tensor(const std::shared_ptr<oneflow::one::Tensor>& tensor) : tensor_(tensor) {}

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

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

Shape Tensor::shape() const {
const auto shape_ = tensor_->shape();
return Shape(std::vector<int64_t>(shape_->dim_vec().begin(), shape_->dim_vec().end()));
}

const Device Tensor::device() const {
Device Tensor::device() const {
const auto device_ = tensor_->device().GetOrThrow();
return Device(device_->type(), device_->device_id());
}

const DType Tensor::dtype() const { return static_cast<DType>(tensor_->dtype()->data_type()); }
DType Tensor::dtype() const { return static_cast<DType>(tensor_->dtype()->data_type()); }

void Tensor::zeros_() {
std::shared_ptr<of::one::MirroredTensor> local_tensor =
Expand Down
17 changes: 13 additions & 4 deletions oneflow/api/cpp/framework/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,18 @@ 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);
[[nodiscard]] const Shape shape() const;
[[nodiscard]] const Device device() const;
[[nodiscard]] const DType dtype() const;

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

~Tensor() = default;

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

[[nodiscard]] Shape shape() const;
[[nodiscard]] Device device() const;
[[nodiscard]] DType dtype() const;

void zeros_();

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

0 comments on commit 4b5eafb

Please sign in to comment.