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

Support Arange delta to decide dtype #6998

Merged
merged 5 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions oneflow/core/functional/functional_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -536,17 +536,17 @@

- name: "arange"
signature: [
"Tensor (Scalar start, Scalar end, Scalar step=1, *, DataType dtype=kInt64,
"Tensor (Scalar start, Scalar end, Scalar step=1, *, DataType dtype=None,
Device device=None) => Arange",
"Tensor (Scalar end, *, DataType dtype=kInt64, Device device=None) => Arange",
"Tensor (Scalar end, *, DataType dtype=None, Device device=None) => Arange",
]
bind_python: True

- name: "consistent_arange"
signature: [
"Tensor (Scalar start, Scalar end, Scalar step=1, *, DataType dtype=kInt64,
"Tensor (Scalar start, Scalar end, Scalar step=1, *, DataType dtype=None,
Placement placement, SbpList sbp) => ConsistentArange",
"Tensor (Scalar end, *, DataType dtype=kInt64, Placement placement, SbpList sbp) => ConsistentArange",
"Tensor (Scalar end, *, DataType dtype=None, Placement placement, SbpList sbp) => ConsistentArange",
]
bind_python: True

Expand Down
74 changes: 52 additions & 22 deletions oneflow/core/functional/impl/math_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -678,19 +678,34 @@ class ArangeFunctor {
public:
ArangeFunctor() { op_ = CHECK_JUST(one::OpBuilder("arange").Output("out").Build()); }
Maybe<Tensor> operator()(const Scalar& start, const Scalar& limit, const Scalar& delta,
const Symbol<DType>& dtype,
const Optional<Symbol<DType>>& dtype,
const Optional<Symbol<Device>>& device) const {
MutableAttrMap attrs;
const DataType range_dtype = dtype->data_type();
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
if (IsIntegralDataType(range_dtype)) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
if (dtype.has_value()) {
const DataType range_dtype = JUST(dtype)->data_type();
if (IsIntegralDataType(range_dtype)) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
}
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
if (delta.IsIntegral()) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
JUST(attrs.SetAttr<DataType>("dtype", DType::Int64()->data_type()));
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
JUST(attrs.SetAttr<DataType>("dtype", DType::Float()->data_type()));
}
}
OpExprInterpContext ctx(attrs);
ctx.device = device;
Expand All @@ -703,7 +718,7 @@ class ArangeFunctor {

class Arange2Functor {
public:
Maybe<Tensor> operator()(const Scalar& limit, const Symbol<DType>& dtype,
Maybe<Tensor> operator()(const Scalar& limit, const Optional<Symbol<DType>>& dtype,
const Optional<Symbol<Device>>& device) const {
return Arange(Scalar(0), limit, Scalar(1), dtype, device);
}
Expand All @@ -713,21 +728,36 @@ class ConsistentArangeFunctor {
public:
ConsistentArangeFunctor() { op_ = CHECK_JUST(one::OpBuilder("arange").Output("out").Build()); }
Maybe<Tensor> operator()(const Scalar& start, const Scalar& limit, const Scalar& delta,
const Symbol<DType>& dtype, const Symbol<ParallelDesc>& placement,
const Optional<Symbol<DType>>& dtype,
const Symbol<ParallelDesc>& placement,
const std::vector<Symbol<cfg::SbpParallel>>& sbp_tuple) const {
MutableAttrMap attrs;
const DataType range_dtype = dtype->data_type();
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
if (IsIntegralDataType(range_dtype)) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
if (dtype.has_value()) {
const DataType range_dtype = JUST(dtype)->data_type();
if (IsIntegralDataType(range_dtype)) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
JUST(attrs.SetAttr<DataType>("dtype", range_dtype));
}
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
if (delta.IsIntegral()) {
JUST(attrs.SetAttr<int64_t>("integer_start", JUST(start.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_limit", JUST(limit.As<int64_t>())));
JUST(attrs.SetAttr<int64_t>("integer_delta", JUST(delta.As<int64_t>())));
JUST(attrs.SetAttr<DataType>("dtype", DType::Int64()->data_type()));
} else {
JUST(attrs.SetAttr<double>("float_start", JUST(start.As<double>())));
JUST(attrs.SetAttr<double>("float_limit", JUST(limit.As<double>())));
JUST(attrs.SetAttr<double>("float_delta", JUST(delta.As<double>())));
JUST(attrs.SetAttr<DataType>("dtype", DType::Float()->data_type()));
}
}

if (LazyMode::is_enabled()) {
std::vector<std::string> nd_sbp(sbp_tuple.size());
{
Expand Down
2 changes: 1 addition & 1 deletion python/oneflow/nn/modules/arange.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def arange_op(
start: int = 0,
end: int = None,
step: int = 1,
dtype: flow.dtype = flow.int64,
dtype: flow.dtype = None,
device: Union[str, flow.device] = None,
placement: flow.placement = None,
sbp: Union[flow.sbp.sbp, List[flow.sbp.sbp]] = None,
Expand Down
10 changes: 10 additions & 0 deletions python/oneflow/test/modules/test_arange.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def test_arange_with_random_data(test_case):
x.to(device)
return x

@autotest(n=5, auto_backward=False, rtol=1e-5, atol=1e-5, check_graph=False)
def test_arange_with_float_delta(test_case):
start = random().to(int)
end = start + random().to(int)
step = random(0, end - start).to(float)
x = torch.arange(start=start, end=end, step=step)
device = random_device()
x.to(device)
return x

def test_consistent_naive(test_case):
placement = flow.placement("cpu", {0: [0]})
sbp = (flow.sbp.broadcast,)
Expand Down