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

Remove almost all usage of Blob in EagerBlobObject #7895

Merged
merged 42 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7ae55d7
remove blob from eager blob object
daquexian Mar 25, 2022
15c31aa
add 'Blob()' in EagerBlobObject, remove BlobObject
daquexian Mar 25, 2022
03f9cc3
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Mar 29, 2022
5078fee
pass ShapeView by value, remove O(1) elem_cnt() feature of Shape
daquexian Mar 29, 2022
6114b70
address comments
daquexian Mar 29, 2022
18215dc
auto format by CI
oneflow-ci-bot Mar 29, 2022
578c517
remove unused variable
daquexian Mar 30, 2022
6861ae1
Merge branch 'master' into remove_blob_from_eager_blob_object
Flowingsun007 Mar 30, 2022
ca98aad
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 30, 2022
70d2563
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 30, 2022
da8d573
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 30, 2022
46a81a6
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 30, 2022
b316e0d
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 30, 2022
a1dc613
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 31, 2022
28baae1
Merge branch 'master' into remove_blob_from_eager_blob_object
Flowingsun007 Mar 31, 2022
56b0d70
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 31, 2022
85e5ef0
Merge branch 'master' into remove_blob_from_eager_blob_object
Flowingsun007 Mar 31, 2022
8fd908a
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 31, 2022
6b7ee0a
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 31, 2022
f03aa21
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Mar 31, 2022
51dde5e
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 1, 2022
74339a3
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 1, 2022
6c29d27
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 1, 2022
c1aae3f
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 1, 2022
d7b6410
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 3, 2022
3f346bd
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 3, 2022
aef13c0
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 5, 2022
8733a21
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 6, 2022
70c0e1a
fix static check error
daquexian Apr 6, 2022
d365d43
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 28, 2022
610967c
fix compile error
daquexian Apr 28, 2022
5bc9db2
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 28, 2022
91f2783
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 29, 2022
3830cfe
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
8713aaf
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
e57e40c
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
7055628
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian Apr 29, 2022
59e9f34
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
23cfba7
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
fe855c0
Merge branch 'master' into remove_blob_from_eager_blob_object
mergify[bot] Apr 29, 2022
267f901
Merge branch 'master' into remove_blob_from_eager_blob_object
daquexian May 3, 2022
75d06ba
fix merge bug
daquexian May 3, 2022
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
2 changes: 0 additions & 2 deletions oneflow/api/python/functional/tensor_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ class LocalTensorSharedNumpyDataFunctor {
JUST(tensor_impl->InitEagerBlobObject(NewLocalDepObject()));
const auto& stream = GetDefaultStreamByDevice(device);
JUST(tensor_impl->eager_blob_object())->set_last_used_stream(stream);
JUST(JUST(tensor_impl->eager_blob_object())->TryInitBlob());
JUST(tensor_impl->eager_blob_object())->mut_blob()->reset_dptr(static_cast<char*>(data_ptr));
std::shared_ptr<Tensor> out(new MirroredTensor(tensor_impl));
return out;
}
Expand Down
6 changes: 3 additions & 3 deletions oneflow/api/python/utils/tensor_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ MaybeGetTensorBufferShapesAndDTypes(const std::shared_ptr<Tensor>& t) {
}));
JUST(btb->WaitUntilCntEqualZero(VirtualMachine::GetPredicatorNoMoreInstructionsFinished()));

const Blob& blob = JUST(tensor->eager_blob_object())->blob();
const Shape& blob_shape = blob.static_shape();
const auto* tensor_buffer_ptr = blob.dptr<TensorBuffer>();
const auto& eager_blob_object = JUST(tensor->eager_blob_object());
const Shape& blob_shape = eager_blob_object->shape();
const auto* tensor_buffer_ptr = eager_blob_object->dptr<TensorBuffer>();
for (int64_t i = 0; i < blob_shape.elem_cnt(); ++i) {
const TensorBuffer* tensor_buffer = tensor_buffer_ptr + i;
shapes.emplace_back(tensor_buffer->shape());
Expand Down
25 changes: 7 additions & 18 deletions oneflow/core/common/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,31 @@ int64_t ShiftNegativeAxis(int64_t axis, const int64_t num_axes) {
return axis;
}

Shape::Shape(const std::initializer_list<int64_t>& dim_vec) : dim_vec_(dim_vec) { UpdateElemCnt(); }
Shape::Shape(const DimVector& dim_vec) : dim_vec_(dim_vec) { UpdateElemCnt(); }
Shape::Shape(DimVector&& dim_vec) : dim_vec_(std::move(dim_vec)) { UpdateElemCnt(); }
Shape::Shape(const ShapeProto& shape_proto) {
Shape::Shape(const std::initializer_list<int64_t>& dim_vec)
: dim_vec_(dim_vec), is_initialized_(true) {}
Shape::Shape(const DimVector& dim_vec) : dim_vec_(dim_vec), is_initialized_(true) {}
Shape::Shape(DimVector&& dim_vec) : dim_vec_(std::move(dim_vec)), is_initialized_(true) {}
Shape::Shape(const ShapeProto& shape_proto) : is_initialized_(true) {
dim_vec_.assign(shape_proto.dim().begin(), shape_proto.dim().end());
UpdateElemCnt();
}
Shape::Shape(const cfg::ShapeProto& shape_proto) {
Shape::Shape(const cfg::ShapeProto& shape_proto) : is_initialized_(true) {
dim_vec_.assign(shape_proto.dim().begin(), shape_proto.dim().end());
UpdateElemCnt();
}

Shape& Shape::operator=(const Shape& shape) {
dim_vec_ = shape.dim_vec_;
UpdateElemCnt();
is_initialized_ = shape.is_initialized_;
return *this;
}

Shape& Shape::assign(const DimVector& dim_vec) {
dim_vec_ = dim_vec;
UpdateElemCnt();
return *this;
}

Shape& Shape::CheckNumAxesIdenticalAndAssign(const ShapeView& shape_view) {
CHECK_EQ(NumAxes(), shape_view.NumAxes());
std::copy(shape_view.ptr(), shape_view.ptr() + shape_view.NumAxes(), dim_vec_.data());
UpdateElemCnt();
return *this;
}

Expand All @@ -97,7 +94,6 @@ Shape& Shape::LeftOnesExtendedAssign(const ShapeView& shape_view) {
FOR_RANGE(int, i, 0, left_ones_size) { dim_vec_.at(i) = 1LL; }
std::copy(shape_view.ptr(), shape_view.ptr() + shape_view.NumAxes(),
dim_vec_.data() + left_ones_size);
UpdateElemCnt();
return *this;
}

Expand Down Expand Up @@ -133,7 +129,6 @@ void Shape::Set(int64_t index, int64_t val) {
CHECK_LT(index, this->NumAxes()) << " Shape: " << DebugStr() << " visit index: " << index
<< " > num_axes: " << this->NumAxes();
dim_vec_.at(index) = val;
UpdateElemCnt();
}

int64_t Shape::Count(int64_t begin_axis, int64_t end_axis) const {
Expand All @@ -146,12 +141,6 @@ int64_t Shape::Count(int64_t begin_axis, int64_t end_axis) const {

int64_t Shape::Count(int64_t begin_axis) const { return Count(begin_axis, NumAxes()); }

void Shape::UpdateElemCnt() {
int64_t elem_cnt = 1;
for (int64_t s : dim_vec_) { elem_cnt *= s; }
elem_cnt_ = elem_cnt;
}

std::ostream& operator<<(std::ostream& out, const Shape& shape) {
out << shape.DebugStr();
return out;
Expand Down
18 changes: 12 additions & 6 deletions oneflow/core/common/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
#define ONEFLOW_CORE_COMMON_SHAPE_H_

#include "oneflow/core/common/shape.pb.h"
#include "oneflow/core/common/shape_view.h"
#include "oneflow/core/common/util.h"
#include "oneflow/core/common/maybe.h"
#include "oneflow/core/common/shape_vec.h"
Expand All @@ -33,7 +34,7 @@ class ShapeProto;
class Shape final {
public:
// OF_DISALLOW_COPY_AND_MOVE(Shape);
Shape() = default;
Shape() : is_initialized_(false) {}
explicit Shape(const DimVector& dim_vec);
explicit Shape(DimVector&& dim_vec);
explicit Shape(const ShapeProto& shape_proto);
Expand All @@ -56,9 +57,12 @@ class Shape final {
void SerializeWithTextFormat(StreamT& out_stream) const;

// Getters and Setters
bool is_initialized() const { return elem_cnt_.has_value(); }
bool is_initialized() const { return is_initialized_; }
const DimVector& dim_vec() const { return dim_vec_; }
int64_t elem_cnt() const { return CHECK_JUST(elem_cnt_); }
DimVector& dim_vec() { return dim_vec_; }
int64_t elem_cnt() const {
return std::accumulate(dim_vec_.begin(), dim_vec_.end(), 1, std::multiplies<int>());
Flowingsun007 marked this conversation as resolved.
Show resolved Hide resolved
}
int64_t At(int64_t index) const;
void Set(int64_t index, int64_t val);
int64_t NumAxes() const {
Expand All @@ -78,11 +82,13 @@ class Shape final {

Maybe<Shape> Slice(int64_t start_dim, int64_t end_dim) const;

private:
void UpdateElemCnt();
ShapeView ToShapeView() const { return ShapeView(dim_vec_.data(), dim_vec_.size()); }

MutShapeView ToMutShapeView() { return MutShapeView(dim_vec_.data(), dim_vec_.size()); }

private:
DimVector dim_vec_;
Optional<int64_t> elem_cnt_;
bool is_initialized_;
};

int64_t ShiftNegativeAxis(int64_t axis, const int64_t num_axes);
Expand Down
3 changes: 3 additions & 0 deletions oneflow/core/common/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ inline double GetCurTime() {
const size_t kHostAlignSize = 64;
const size_t kCudaAlignSize = 512;
const size_t kCudaMemAllocAlignSize = 512;
const int32_t kBlobBodyAlignSize = 512;
const int32_t kBlobHeaderAlignSize = 64;

inline size_t RoundUp(size_t n, size_t val) { return (n + val - 1) / val * val; }

inline size_t GetCudaAlignedSize(size_t size) { return RoundUp(size, kCudaAlignSize); }
Expand Down
27 changes: 2 additions & 25 deletions oneflow/core/eager/blob_instruction_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,42 +19,18 @@ limitations under the License.
#include "oneflow/core/vm/instruction.h"
#include "oneflow/core/vm/instruction_type.h"
#include "oneflow/core/eager/blob_instruction_type.h"
#include "oneflow/core/eager/blob_object.h"
#include "oneflow/core/vm/control_stream_type.h"
#include "oneflow/core/vm/stream.h"
#include "oneflow/core/device/cuda_util.h"
#include "oneflow/core/register/register_manager.h"
#include "oneflow/core/eager/lazy_ref_blob_object.h"
#include "oneflow/core/operator/operator.h"
#include "oneflow/core/vm/access_blob_arg_cb_phy_instr_operand.h"
#include "oneflow/core/register/ofblob.h"
#include "oneflow/core/eager/eager_blob_object.h"
#include "oneflow/core/vm/tensor_view_operand.h"

namespace oneflow {
namespace vm {

void TensorViewInstructionType::Compute(vm::Instruction* instruction) const {
return ComputeInstrMsg(instruction->instr_msg());
}

void TensorViewInstructionType::ComputeInFuseMode(vm::InstructionMsg* instr_msg) const {
return ComputeInstrMsg(*instr_msg);
}

void TensorViewInstructionType::ComputeInstrMsg(const vm::InstructionMsg& instr_msg) const {
const auto& phy_instr_operand = instr_msg.phy_instr_operand();
CHECK(static_cast<bool>(phy_instr_operand));
const auto* ptr = dynamic_cast<const vm::TensorViewOperand*>(phy_instr_operand.get());
CHECK_NOTNULL(ptr);
DeviceCtx* device_ctx = instr_msg.phy_instr_stream()->device_ctx().get();
OfBlob input_ofblob(device_ctx->stream(), ptr->eager_blob_object()->mut_blob());
OfBlob view_ofblob(device_ctx->stream(), ptr->view_eager_blob_object()->mut_blob());

void* input_ptr = input_ofblob.mut_blob()->mut_raw_dptr();
view_ofblob.mut_blob()->reset_dptr(static_cast<char*>(input_ptr));
}

void AccessBlobByCallbackInstructionType::Compute(vm::Instruction* instruction) const {
return ComputeInstrMsg(instruction->instr_msg());
}
Expand All @@ -71,7 +47,8 @@ void AccessBlobByCallbackInstructionType::ComputeInstrMsg(
dynamic_cast<const vm::AccessBlobArgCbPhyInstrOperand*>(phy_instr_operand.get());
CHECK_NOTNULL(ptr);
DeviceCtx* device_ctx = instr_msg.phy_instr_stream()->device_ctx().get();
OfBlob ofblob(device_ctx->stream(), ptr->eager_blob_object()->mut_blob());
auto* blob = ptr->eager_blob_object()->blob();
OfBlob ofblob(device_ctx->stream(), blob);
ptr->callback()(reinterpret_cast<uint64_t>(&ofblob));
}

Expand Down
12 changes: 0 additions & 12 deletions oneflow/core/eager/blob_instruction_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ limitations under the License.
namespace oneflow {
namespace vm {

class TensorViewInstructionType : public vm::InstructionType {
public:
TensorViewInstructionType() = default;
~TensorViewInstructionType() override = default;

void Compute(vm::Instruction* instruction) const override;
void ComputeInFuseMode(vm::InstructionMsg* instr_msg) const override;

private:
void ComputeInstrMsg(const vm::InstructionMsg& instr_msg) const;
};

class AccessBlobByCallbackInstructionType : public vm::InstructionType {
public:
AccessBlobByCallbackInstructionType() = default;
Expand Down
43 changes: 0 additions & 43 deletions oneflow/core/eager/blob_object.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions oneflow/core/eager/blob_object.h

This file was deleted.

9 changes: 0 additions & 9 deletions oneflow/core/eager/cpu_blob_instruction_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,5 @@ class CpuAccessBlobByCallbackInstructionType final : public AccessBlobByCallback
COMMAND(vm::RegisterInstructionType<CpuAccessBlobByCallbackInstructionType>(
"cpu.AccessBlobByCallback"));

class CpuTensorViewInstructionType final : public TensorViewInstructionType {
public:
CpuTensorViewInstructionType() = default;
~CpuTensorViewInstructionType() override = default;

using stream_type = vm::CpuStreamType;
};
COMMAND(vm::RegisterInstructionType<CpuTensorViewInstructionType>("cpu.TensorView"));

} // namespace vm
} // namespace oneflow
1 change: 0 additions & 1 deletion oneflow/core/eager/cpu_opkernel_instruction_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License.
*/
#include "oneflow/core/common/util.h"
#include "oneflow/core/job/job_desc.h"
#include "oneflow/core/eager/blob_object.h"
#include "oneflow/core/eager/opkernel_instruction_type.h"
#include "oneflow/core/vm/stream.h"
#include "oneflow/core/vm/cpu_stream_type.h"
Expand Down
Loading