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

symbolic shape #9902

Open
wants to merge 80 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
6658783
init InterpretJob
daquexian Feb 19, 2023
69decc3
support sd
daquexian Feb 20, 2023
0314b66
cache op exprs
daquexian Feb 21, 2023
96addf4
refine, add test
daquexian Feb 21, 2023
f543907
Merge branch 'master' into job_vm
daquexian Feb 22, 2023
f527293
refine
daquexian Feb 22, 2023
0e7097c
Merge branch 'master' into job_vm
mergify[bot] Feb 23, 2023
ed42968
add more comments
daquexian Feb 23, 2023
2179698
auto format by CI
oneflow-ci-bot Feb 23, 2023
fa3e6f1
rename dead_tensors -> outdated_tensors_after_op
daquexian Feb 23, 2023
6a99a3c
auto format by CI
oneflow-ci-bot Feb 23, 2023
bb0587e
init symbolic shape
daquexian Feb 19, 2023
92f0f5d
update ShapeProto and other methods, support mlir
daquexian Feb 23, 2023
9f2004f
refine
daquexian Feb 24, 2023
6f926a3
get it running
daquexian Feb 24, 2023
0774994
refine
daquexian Feb 24, 2023
ea2f159
compile all kernels, add graph._input_shape interface
daquexian Feb 24, 2023
56b6934
set an empty stride in lazy mode, add a test
daquexian Feb 24, 2023
e74fdcc
update tests
daquexian Feb 24, 2023
686466f
auto format by CI
oneflow-ci-bot Feb 24, 2023
8363de3
Merge branch 'master' into sym_shape
daquexian Feb 25, 2023
3e21fcd
auto format by CI
oneflow-ci-bot Feb 25, 2023
623b13e
Merge branch 'master' into sym_shape
daquexian Mar 2, 2023
956f4a1
refine initializer construction
daquexian Mar 2, 2023
0535934
auto format by CI
oneflow-ci-bot Mar 2, 2023
6b0b90e
refine
daquexian Mar 3, 2023
2ab5fa5
not build output buffer if running by vm, update binary operators of Dim
daquexian Mar 3, 2023
2eda797
Merge branch 'sym_shape' of github.com:Oneflow-Inc/oneflow into sym_s…
daquexian Mar 3, 2023
25b4c35
auto format by CI
oneflow-ci-bot Mar 3, 2023
9bbdce5
Merge branch 'master' into sym_shape
daquexian Mar 3, 2023
fd0718c
fix compile error, refine
daquexian Mar 3, 2023
d8bb05a
restore unused changes
daquexian Mar 3, 2023
bc41dad
auto format by CI
oneflow-ci-bot Mar 3, 2023
306adfa
fix compile errors in .cu files
daquexian Mar 3, 2023
eb3b260
auto format by CI
oneflow-ci-bot Mar 3, 2023
e50ce5b
ptr() and data() returns int64_t*, restore changes in some kernels, r…
daquexian Mar 9, 2023
4afb09c
auto format by CI
oneflow-ci-bot Mar 9, 2023
b99fe9a
restore more changes
daquexian Mar 9, 2023
8b415ee
fix static analysis errors
daquexian Mar 9, 2023
be35417
ShapeView::At and operator[] returns int64_t
daquexian Mar 10, 2023
8f62106
Merge branch 'master' into sym_shape
daquexian Mar 10, 2023
5c5361d
auto format by CI
oneflow-ci-bot Mar 10, 2023
712d329
refine some comments
daquexian Mar 10, 2023
2f75e05
Merge branch 'master' into sym_shape
daquexian Mar 13, 2023
d861265
add Shape::Shape(vector<int64_t>)
daquexian Mar 13, 2023
a1e2d99
Merge branch 'master' into sym_shape
mergify[bot] Mar 14, 2023
8751331
fix InplaceExpand
daquexian Mar 14, 2023
210e289
update graph.py
daquexian Mar 14, 2023
bfff72e
auto format by CI
oneflow-ci-bot Mar 14, 2023
4a096ce
Merge branch 'master' into sym_shape
mergify[bot] Mar 14, 2023
ffe998a
fix cpp api
daquexian Mar 15, 2023
a5af0a2
auto format by CI
oneflow-ci-bot Mar 15, 2023
9ab4a7d
Merge branch 'master' into sym_shape
daquexian Mar 15, 2023
16f3344
Merge branch 'master' into sym_shape
mergify[bot] Mar 15, 2023
ae14534
fix mlir tests
daquexian Mar 16, 2023
a9c5509
Merge branch 'master' into sym_shape
daquexian Mar 16, 2023
59a20e4
auto format by CI
oneflow-ci-bot Mar 16, 2023
c495faf
fix shape constructor errors in clang
daquexian Mar 17, 2023
dd9535c
Merge branch 'sym_shape' of github.com:Oneflow-Inc/oneflow into sym_s…
daquexian Mar 17, 2023
c26365a
Merge branch 'master' into sym_shape
daquexian Mar 17, 2023
426fbc1
Merge branch 'master' into sym_shape
mergify[bot] Mar 17, 2023
4b66c14
fix more clang errors
daquexian Mar 18, 2023
2451357
Merge branch 'master' into sym_shape
mergify[bot] Mar 18, 2023
e292e6e
Revert "fix more clang errors"
daquexian Mar 20, 2023
8ac21c9
Revert "fix shape constructor errors in clang"
daquexian Mar 20, 2023
999e600
add Shape::Shape(std::initializer_list<int64_t>) to fix clang compile…
daquexian Mar 20, 2023
2e8a117
Merge branch 'sym_shape' of github.com:Oneflow-Inc/oneflow into sym_s…
daquexian Mar 20, 2023
939197c
Merge branch 'master' into sym_shape
daquexian Mar 20, 2023
9a24a2b
Merge branch 'master' into sym_shape
mergify[bot] Mar 20, 2023
b2f4c86
Merge branch 'master' into sym_shape
mergify[bot] Mar 20, 2023
161a395
only expose Shape::Shape(std::initializer_list<int64_t>) for clang
daquexian Mar 21, 2023
d3848c5
fix compile errors in nvcc
daquexian Mar 22, 2023
7e1298b
Merge branch 'master' into sym_shape
daquexian Mar 22, 2023
096976a
Merge branch 'master' into sym_shape
mergify[bot] Mar 22, 2023
31d71d7
Merge branch 'master' into sym_shape
mergify[bot] Mar 22, 2023
9988db2
Merge branch 'master' into sym_shape
mergify[bot] Mar 22, 2023
fa5bfdf
avoid compile error in CI
daquexian Mar 23, 2023
4a2ce42
Merge branch 'master' into sym_shape
mergify[bot] Mar 23, 2023
a0c8a4e
Merge branch 'master' into sym_shape
mergify[bot] Mar 23, 2023
f1bc5da
Merge branch 'master' into sym_shape
mergify[bot] Mar 23, 2023
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
10 changes: 7 additions & 3 deletions oneflow/api/cpp/framework/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,12 @@ of::Maybe<void> Graph::GraphImpl::AddOp(of::OperatorConf op_conf) {
}
op_conf.set_device_tag(GetDeviceTag(device_));
if (batch_size_ > 0 && op_conf.has_input_conf()) {
op_conf.mutable_input_conf()->mutable_blob_conf()->mutable_shape()->mutable_dim()->Set(
0, batch_size_);
op_conf.mutable_input_conf()
->mutable_blob_conf()
->mutable_shape()
->mutable_dim()
->Add()
->set_int64_value(batch_size_);
}
auto* ctx = JUST(of::GetCurInferCtx());
JUST(ctx->AddAndInferGlobalOp(op_conf));
Expand Down Expand Up @@ -379,7 +383,7 @@ of::Maybe<void> Graph::GraphImpl::BuildGraph() {
const std::string input_lbi_str = op_conf.output_conf().in();
const of::LogicalBlobId input_lbi = of::GenLogicalBlobId(input_lbi_str);
int64_t batch_size = node->LogicalBlobDesc4Lbi(input_lbi).shape().At(0);
blob_conf.mutable_shape()->set_dim(0, batch_size);
blob_conf.mutable_shape()->mutable_dim()->Add()->set_int64_value(batch_size);
}
output_name_to_tensor_[op_conf.name()] = JUST(of::one::functional::Empty(
of::Shape(blob_conf.shape()),
Expand Down
65 changes: 65 additions & 0 deletions oneflow/api/python/framework/dim.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 <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include "oneflow/api/python/of_api_registry.h"
#include "oneflow/core/common/dim.h"

namespace py = pybind11;

namespace oneflow {

#define DEFINE_OPERATOR(op) \
.def(py::self op py::self) \
.def(py::self op char()) \
.def(py::self op static_cast<unsigned char>(0)) \
.def(py::self op int()) \
.def(py::self op static_cast<unsigned int>(0)) \
.def(py::self op long()) \
.def(py::self op 0UL) \
.def(py::self op 0LL) \
.def(py::self op 0ULL) \
.def(py::self op float()) \
.def(py::self op double())

ONEFLOW_API_PYBIND11_MODULE("", m) {
py::class_<Dim>(m, "Dim")
.def(py::init([](int value) { return Dim(value); }))
.def_static("unknown", &Dim::Unknown)
.def("__str__",
[](Dim dim) {
std::stringstream ss;
ss << dim;
return ss.str();
})
.def("__repr__",
[](Dim dim) {
std::stringstream ss;
ss << dim;
return ss.str();
})
// clang-format off
DEFINE_OPERATOR(==)
DEFINE_OPERATOR(!=)
DEFINE_OPERATOR(<)
DEFINE_OPERATOR(<=)
DEFINE_OPERATOR(>)
DEFINE_OPERATOR(>=)
// clang-format on
.def(py::hash(py::self));
}

} // namespace oneflow
34 changes: 24 additions & 10 deletions oneflow/api/python/framework/size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static PyObject* TensorSize_repr(TensorSize* self) {
int32_t size = PyTuple_Size((PyObject*)self);
ss << "oneflow.Size([";
for (int i = 0; i < size; ++i) {
int64_t dim = PyLong_AsLongLong(PyTuple_GET_ITEM(self, i));
Dim dim = one::functional::PyUnpackShapeItem(PyTuple_GET_ITEM(self, i));
ss << dim;
if (++idx != size) { ss << ", "; }
}
Expand All @@ -44,10 +44,11 @@ static PyObject* TensorSize_new(PyTypeObject* type, PyObject* args, PyObject* kw
if (self.get()) {
for (int i = 0; i < PyTuple_Size(self.get()); ++i) {
PyObject* item = PyTuple_GET_ITEM(self.get(), i);
if (!PyLong_Check(item)) {
return PyErr_Format(PyExc_TypeError,
"oneflow.Size() takes an iterable of 'int', but item '%d' is '%s'", i,
Py_TYPE(item)->tp_name);
if (!one::functional::PyShapeItemCheck(item)) {
return PyErr_Format(
PyExc_TypeError,
"oneflow.Size() takes an iterable of 'int' or 'Dim', but item '%d' is '%s'", i,
Py_TYPE(item)->tp_name);
}
}
}
Expand Down Expand Up @@ -121,8 +122,18 @@ static PyObject* TensorSize_numel(PyObject* self, PyObject* args) {
return PyLong_FromLongLong(numel);
}

static PyObject* TensorSize_all_dims_known(PyObject* self, PyObject* args) {
for (int i = 0; i < PyTuple_Size(self); ++i) {
Dim dim = one::functional::PyUnpackShapeItem(PyTuple_GET_ITEM((TensorSize*)self, i));
if (!dim.is_known()) { Py_RETURN_FALSE; }
}
Py_RETURN_TRUE;
}

static PyMethodDef TensorSize_methods[] = {
{"numel", (PyCFunction)TensorSize_numel, METH_NOARGS, NULL}, {NULL}};
{"numel", (PyCFunction)TensorSize_numel, METH_NOARGS, NULL},
{"all_dims_known", (PyCFunction)TensorSize_all_dims_known, METH_NOARGS, NULL},
{NULL}};

PyTypeObject TensorSize_Type = {
PyVarObject_HEAD_INIT(NULL, 0) "oneflow.Size", /* tp_name */
Expand Down Expand Up @@ -173,7 +184,10 @@ PyObject* TensorSize_NewFromShape(const Shape& size) {
PyObjectPtr self(TensorSize_New(size.NumAxes()));
if (self.get()) {
for (int i = 0; i < size.NumAxes(); ++i) {
PyTuple_SET_ITEM(self.get(), i, PyLong_FromLongLong(size.At(i)));
Dim dim = size[i];
PyTuple_SET_ITEM(self.get(), i,
dim.is_known() ? one::functional::CastToPyObject(dim.val())
: one::functional::CastToPyObject(dim));
}
}
return self.release();
Expand All @@ -186,11 +200,11 @@ Shape TensorSize_AsShape(PyObject* self) {
return Shape();
}
int size = TensorSize_length((TensorSize*)self);
DimVector dim_vec(size);
Shape shape(size);
for (int i = 0; i < size; ++i) {
dim_vec[i] = PyLong_AsLongLong(PyTuple_GET_ITEM((TensorSize*)self, i));
shape[i] = py::handle(PyTuple_GET_ITEM((TensorSize*)self, i)).cast<Dim>();
}
return Shape(std::move(dim_vec));
return shape;
}

ONEFLOW_API_PYBIND11_MODULE("", m) {
Expand Down
35 changes: 30 additions & 5 deletions oneflow/api/python/functional/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,50 @@ std::vector<Symbol<DType>> PyUnpackDTypeSequence(PyObject* obj) {
return PyUnpackSequence<Symbol<DType>>(obj, [](PyObject* item) { return PyUnpackDType(item); });
}

bool PyDimCheck(PyObject* obj) { return detail::isinstance_fast<Dim>(obj); }
Dim PyUnpackDim(PyObject* obj) { return *detail::cast_fast<Dim*>(obj); }

// Shape
bool PyShapeCheck(PyObject* obj) { return PyLongSequenceCheck(obj); }
bool PyShapeItemCheck(PyObject* obj) {
return PyLong_Check(obj) || PyIntegerScalarTensorCheck(obj) || PyDimCheck(obj);
}
Dim PyUnpackShapeItem(PyObject* obj) {
if (PyLong_Check(obj)) {
return PyLong_AsLongLong(obj);
} else if (PyDimCheck(obj)) {
return PyUnpackDim(obj);
} else {
return PyUnpackIntegerScalarTensor_AsLongLong(obj);
}
}
bool PyShapeCheck(PyObject* obj) {
return PySequenceCheck(obj, [](PyObject* item) { return PyShapeItemCheck(item); });
}

Shape PyUnpackShape(PyObject* obj) {
bool is_tuple = PyTuple_Check(obj);
CHECK_OR_THROW(is_tuple || PyList_Check(obj))
<< "The object is not list or tuple, but is " << Py_TYPE(obj)->tp_name;
size_t size = is_tuple ? PyTuple_GET_SIZE(obj) : PyList_GET_SIZE(obj);
DimVector values(size);
Shape shape(size);
for (int i = 0; i < size; ++i) {
PyObject* item = is_tuple ? PyTuple_GET_ITEM(obj, i) : PyList_GET_ITEM(obj, i);
values[i] = PyLong_AsLongLong(item);
if (PyLong_Check(item)) {
shape.Set(i, PyLong_AsLongLong(item));
} else if (PyIntegerScalarTensorCheck(item)) {
shape.Set(i, PyUnpackIntegerScalarTensor_AsLongLong(item));
} else if (PyDimCheck(item)) {
shape.Set(i, PyUnpackDim(item));
} else {
THROW(RuntimeError) << "Unsupported item type: " << Py_TYPE(item)->tp_name;
}
}
return Shape(values);
return shape;
}

// Shape list
bool PyShapeSequenceCheck(PyObject* obj) {
return PySequenceCheck(obj, [](PyObject* item) { return PyLongSequenceCheck(item); });
return PySequenceCheck(obj, [](PyObject* item) { return PyShapeCheck(item); });
}
std::vector<Shape> PyUnpackShapeSequence(PyObject* obj) {
return PyUnpackSequence<Shape>(obj, [](PyObject* item) -> Shape { return PyUnpackShape(item); });
Expand Down
9 changes: 7 additions & 2 deletions oneflow/api/python/functional/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ limitations under the License.
#include <pybind11/pybind11.h>

#include "oneflow/api/python/framework/tensor.h"
#include "oneflow/api/python/caster/maybe.h"
#include "oneflow/api/python/caster/optional.h"
#include "oneflow/api/python/of_api_registry.h"
#include "oneflow/core/common/throw.h"
#include "oneflow/core/common/maybe.h"
#include "oneflow/core/common/preprocessor.h"
Expand Down Expand Up @@ -166,7 +165,13 @@ Symbol<MemoryFormat> PyUnpackMemoryFormat(PyObject* obj);
bool PyDTypeSequenceCheck(PyObject* obj);
std::vector<Symbol<DType>> PyUnpackDTypeSequence(PyObject* obj);

// Dim
bool PyDimCheck(PyObject* obj);
Dim PyUnpackDim(PyObject* obj);

// Shape
bool PyShapeItemCheck(PyObject* obj);
Dim PyUnpackShapeItem(PyObject* obj);
bool PyShapeCheck(PyObject* obj);
Shape PyUnpackShape(PyObject* obj);

Expand Down
17 changes: 10 additions & 7 deletions oneflow/api/python/functional/dispatch_stateful_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ namespace functional {
namespace impl {

ONEFLOW_FUNCTION_LIBRARY(m) {
m.add_functor(
"DispatchFeedInput",
[](const std::shared_ptr<OpExpr>& op, const std::shared_ptr<Tensor>& input) -> Maybe<Tensor> {
const auto& origin_input = JUST(OpInterpUtil::Dispatch<Tensor>(*op, {input}));
// Unpack input when do grad acc
return GradAccTryInsertUnpackAfterInput(origin_input);
});
m.add_functor("DispatchFeedInput",
[](const std::shared_ptr<OpExpr>& op, const std::shared_ptr<Tensor>& input,
const Optional<Shape>& shape) -> Maybe<Tensor> {
const auto& origin_input =
shape.has_value() ? JUST(OpInterpUtil::Dispatch<Tensor>(
*op, {input}, OpExprInterpContext(JUST(shape))))
: JUST(OpInterpUtil::Dispatch<Tensor>(*op, {input}));
// Unpack input when do grad acc
return GradAccTryInsertUnpackAfterInput(origin_input);
});
m.add_functor(
"DispatchFetchOutput",
[](const std::shared_ptr<OpExpr>& op, const std::shared_ptr<Tensor>& input) -> Maybe<Tensor> {
Expand Down
2 changes: 1 addition & 1 deletion oneflow/api/python/functional/dispatch_stateful_ops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# }

- name: "dispatch_feed_input"
signature: "Tensor (OpExpr op, Tensor input) => DispatchFeedInput"
signature: "Tensor (OpExpr op, Tensor input, Shape shape=None) => DispatchFeedInput"
bind_python: True

- name: "dispatch_feed_variable"
Expand Down
2 changes: 1 addition & 1 deletion oneflow/api/python/functional/python_arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ bool PythonArg::TypeCheck(ValueType type) const {
case kDTYPE: return PyDTypeCheck(object_);
case kLAYOUT: return PyLayoutCheck(object_);
case kMEMORYFORMAT: return PyMemoryFormatCheck(object_);
case kSHAPE: return PyLongSequenceCheck(object_);
case kSHAPE: return PyShapeCheck(object_);
case kGENERATOR:
case kGENERATOR_REF: return PyGeneratorCheck(object_);
case kTENSOR_INDEX: return PyTensorIndexCheck(object_);
Expand Down
2 changes: 1 addition & 1 deletion oneflow/core/autograd/gradient_funcs/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Maybe<void> Expand::Capture(ExpandCaptureState* ctx, const TensorTuple& inputs,
if (ctx->keep_dims) {
for (size_t i = 0; i < expand_shape.size(); ++i) {
const auto& t_dim = expand_shape[i];
const auto& dim = i < ctx->lpad ? 1 : in_shape[i - ctx->lpad];
const auto& dim = i < ctx->lpad ? 1 : in_shape[i - ctx->lpad].val();
if (dim != t_dim) { ctx->reduce_dims.push_back(i); }
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions oneflow/core/autograd/gradient_funcs/matmul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Maybe<void> BroadcastMatmul::Capture(BroadcastMatmulCaptureState* ctx, const Ten
const int64_t num_batch_dims = num_dims - 2;
const int64_t num_padding_dims = num_max_batch_dims - num_batch_dims;
return [num_padding_dims, shape_dim](size_t index) {
return index < num_padding_dims ? 1 : shape_dim.At(index - num_padding_dims);
return index < num_padding_dims ? Dim(1) : shape_dim.At(index - num_padding_dims);
};
};
auto GetABatchDim = MakeGetBatchDim(a_num_axes, *a_shape);
Expand Down Expand Up @@ -218,7 +218,7 @@ Maybe<void> BroadcastMatmul::Apply(const BroadcastMatmulCaptureState* ctx,
const int64_t num_batch_dims = num_dims - 2;
const int64_t num_padding_dims = num_max_batch_dims - num_batch_dims;
return [num_padding_dims, shape_dim](size_t index) {
return index < num_padding_dims ? 1 : shape_dim.At(index - num_padding_dims);
return index < num_padding_dims ? Dim(1) : shape_dim.At(index - num_padding_dims);
};
};
auto GetOutBatchDim = MakeGetBatchDim(out_num_axes, *out_shape);
Expand Down
2 changes: 1 addition & 1 deletion oneflow/core/boxing/boxing_dividor_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ Maybe<Symbol<ParallelDesc>> GetFisrtDeviceOfPlacement(Symbol<ParallelDesc> place
parallel_conf.add_device_name(std::string("@") + std::to_string(machine_id) + ":"
+ std::to_string(device_id));
for (int64_t i = 0; i < placement->hierarchy()->NumAxes(); ++i) {
parallel_conf.mutable_hierarchy()->add_dim(1);
parallel_conf.mutable_hierarchy()->add_dim()->set_int64_value(1);
}
std::shared_ptr<ParallelDesc> parallel_desc;
JUST(PhysicalRun([&parallel_desc, &parallel_conf](InstructionsBuilder* builder) -> Maybe<void> {
Expand Down
Loading