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

Add fully support for all datatype #7025

Merged
merged 14 commits into from
Dec 15, 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
12 changes: 9 additions & 3 deletions oneflow/api/python/framework/dtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,25 @@ ONEFLOW_API_PYBIND11_MODULE("", m) {
m.attr("char") = &CHECK_JUST(DType::Get(DataType::kChar));
m.attr("float16") = &CHECK_JUST(DType::Get(DataType::kFloat16));
m.attr("float") = &CHECK_JUST(DType::Get(DataType::kFloat));

m.attr("float32") = &CHECK_JUST(DType::Get(DataType::kFloat));
m.attr("double") = &CHECK_JUST(DType::Get(DataType::kDouble));
m.attr("float64") = &CHECK_JUST(DType::Get(DataType::kDouble));

m.attr("int8") = &CHECK_JUST(DType::Get(DataType::kInt8));
m.attr("int32") = &CHECK_JUST(DType::Get(DataType::kInt32));
m.attr("int64") = &CHECK_JUST(DType::Get(DataType::kInt64));

m.attr("uint8") = &CHECK_JUST(DType::Get(DataType::kUInt8));
m.attr("record") = &CHECK_JUST(DType::Get(DataType::kOFRecord));
m.attr("tensor_buffer") = &CHECK_JUST(DType::Get(DataType::kTensorBuffer));
m.attr("bfloat16") = &CHECK_JUST(DType::Get(DataType::kBFloat16));
m.attr("uint16") = &CHECK_JUST(DType::Get(DataType::kUInt16));
m.attr("uint32") = &CHECK_JUST(DType::Get(DataType::kUInt32));
m.attr("uint64") = &CHECK_JUST(DType::Get(DataType::kUInt64));
m.attr("uint128") = &CHECK_JUST(DType::Get(DataType::kUInt128));
m.attr("int16") = &CHECK_JUST(DType::Get(DataType::kInt16));
m.attr("int128") = &CHECK_JUST(DType::Get(DataType::kInt128));
m.attr("complex32") = &CHECK_JUST(DType::Get(DataType::kComplex32));
m.attr("complex64") = &CHECK_JUST(DType::Get(DataType::kComplex64));
m.attr("complex128") = &CHECK_JUST(DType::Get(DataType::kComplex128));
}

} // namespace oneflow
10 changes: 9 additions & 1 deletion oneflow/core/common/data_type.proto
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ enum DataType {
kTensorBuffer = 10;
kBFloat16 = 11;
kBool = 12;
kMaxDataType = 13;
kUInt16 = 13;
kUInt32 = 14;
kUInt64 = 15;
kUInt128 = 16;
kInt16 = 17;
kInt128 = 18;
kComplex32 = 19;
kComplex64 = 20;
kComplex128 = 21;
}

message OptInt64 {
Expand Down
110 changes: 77 additions & 33 deletions oneflow/core/framework/dtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,33 @@ bool DType::is_complex() const { return CHECK_JUST(DTypeMeta4DataType(data_type_

/*
The order of datatype is:
0 1 2 3 4 5 6 7 8 9 10 11
iv c1 f4 f8 i1 i4 i8 u1 re f2 bu bf
The priority order of datatype is:
0 1 2 3 4 5 6 7 8 9 10 11
iv < u1 < c1 < i1 < i4 < i8 < f2 < f4 < f8 < bf < re < bu.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
20 iv c1 f4 f8 i1 i4 i8 u1 re f2 bu bf b1 u4 u8 u16 i2 i16 cp4
cp8 cp16 The priority order of datatype is: 0 1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 iv < b1 < u1 < c1 < i1 < i2 < u4 < i4 < u8 <
i8 < u16 < i16 < f2 < f4 < f8 < cp4 < cp8 < cp16 < bf < re < bu.
*/
const int DType::priority_order[DataType::kMaxDataType] = {0, /*kInvalid*/
2, /*kChar*/
7, /*kFloat32*/
8, /*kDouble*/
3, /*kInt8*/
4, /*kInt32*/
5, /*kInt64*/
1, /*kUInt8*/
10, /*kOFRecord*/
6, /*kFloat16*/
11, /*kTensorBuffer*/
9 /*kBFloat16*/};
const int DType::priority_order[DataType_ARRAYSIZE] = {0, /*kInvalid*/
3, /*kChar*/
13, /*kFloat32*/
14, /*kDouble*/
4, /*kInt8*/
7, /*kInt32*/
9, /*kInt64*/
2, /*kUInt8*/
19, /*kOFRecord*/
12, /*kFloat16*/
20, /*kTensorBuffer*/
18, /*kBFloat16*/
1, /*kBool*/
6, /*kUint32*/
8, /*kUint64*/
10, /*kUint128*/
5, /*kInt16*/
11, /*kInt128*/
15, /*kComplex32*/
16, /*kComplex64*/
17 /*kComplex128*/};

bool DType::is_floating_point() const {
return CHECK_JUST(DTypeMeta4DataType(data_type_)).is_floating_point();
Expand Down Expand Up @@ -150,6 +159,16 @@ Symbol<DType> promoteTypes(const Symbol<DType> a, const Symbol<DType> b) {
const Symbol<DType> f2 = CHECK_JUST(DType::Get(DataType::kFloat16));
const Symbol<DType> bu = CHECK_JUST(DType::Get(DataType::kTensorBuffer));
const Symbol<DType> bf = CHECK_JUST(DType::Get(DataType::kBFloat16));
const Symbol<DType> b1 = CHECK_JUST(DType::Get(DataType::kBool));
const Symbol<DType> u2 = CHECK_JUST(DType::Get(DataType::kUInt16));
const Symbol<DType> u4 = CHECK_JUST(DType::Get(DataType::kUInt32));
const Symbol<DType> u8 = CHECK_JUST(DType::Get(DataType::kUInt64));
const Symbol<DType> u16 = CHECK_JUST(DType::Get(DataType::kUInt128));
const Symbol<DType> i2 = CHECK_JUST(DType::Get(DataType::kInt16));
const Symbol<DType> i16 = CHECK_JUST(DType::Get(DataType::kInt128));
const Symbol<DType> cp4 = CHECK_JUST(DType::Get(DataType::kComplex32));
const Symbol<DType> cp8 = CHECK_JUST(DType::Get(DataType::kComplex64));
const Symbol<DType> cp16 = CHECK_JUST(DType::Get(DataType::kComplex128));

/* It is consistent with data_type.proto(except kInvalidDataType, kOFRecord and kTensorBuffer)
kInvalidDataType = 0;
Expand All @@ -164,29 +183,54 @@ Symbol<DType> promoteTypes(const Symbol<DType> a, const Symbol<DType> b) {
kFloat16 = 9;
kTensorBuffer = 10;
kBFloat16 = 11;
kBool = 12;
kUInt16 = 13;
kUInt32 = 14;
kUInt64 = 15;
kUInt128 = 16;
kInt16 = 17;
kInt128 = 18;
kComplex32 = 19;
kComplex64 = 20;
kComplex128 = 21;

The priority order of datatype is:
iv < u1 < c1 < i1 < i4 < i8 < f2 < f4 < f8 < bf < re < bu.
iv < b1 < u1 < c1 < i1 < u2 < i2 < u4 < i4 < u8 < i8 < u16 < i16 < f2 < f4 < f8 < cp4 < cp8 <
cp16 < bf < re < bu.

When int8 + uint8, it need to promote to int16, etc.
But in int8 + uint128, we should promote to int256, but it is not exist, so we set as Invalid.

The new DataType should be add in the end of proto, and the Loopup table should be maintained as
right priority (author:zhengzekang).
*/
static const Symbol<DType> _promoteTypesLookup[DataType::kMaxDataType][DataType::kMaxDataType] = {
/* iv c1 f4 f8 i1 i4 i8 u1 re f2 bu bf */
/* iv */ {iv, c1, f4, f8, i1, i4, i8, u1, re, f2, bu, bf},
/* c1 */ {c1, c1, f4, f8, i1, i4, i8, c1, re, f2, bu, bf},
/* f4 */ {f4, f4, f4, f8, f4, f4, f4, f4, re, f4, bu, bf},
/* f8 */ {f8, f8, f8, f8, f8, f8, f8, f8, re, f8, bu, bf},
/* i1 */ {i1, i1, f4, f8, i1, i4, i8, i1, re, f2, bu, bf},
/* i4 */ {i4, i4, f4, f8, i4, i4, i8, i4, re, f2, bu, bf},
/* i8 */ {i8, i8, f4, f8, i8, i8, i8, i8, re, f2, bu, bf},
/* u1 */ {u1, c1, f4, f8, i1, i4, i8, u1, re, f2, bu, bf},
/* re */ {re, re, re, re, re, re, re, re, re, re, bu, re},
/* f2 */ {f2, f2, f4, f8, f2, f2, f2, f2, re, f2, bu, bf},
/* bu */ {bu, bu, bu, bu, bu, bu, bu, bu, bu, bu, bu, bu},
/* bf */ {bf, bf, bf, bf, bf, bf, bf, bf, re, bf, bu, bf},
};

// clang-format off
static const Symbol<DType> _promoteTypesLookup[DataType_ARRAYSIZE][DataType_ARRAYSIZE] = {
/* iv c1 f4 f8 i1 i4 i8 u1 re f2 bu bf b1 u2 u4 u8 u16 i2 i16 cp4 cp8 cp16 */
/* iv */ {iv, c1, f4, f8, i1, i4, i8, u1, re, f2, bu, bf, b1, u2, u4, u8, u16, i2, i16, cp4, cp8, cp16},
/* c1 */ {c1, c1, f4, f8, i1, i4, i8, c1, iv, f2, iv, bf, c1, u2, u4, u8, u16, i2, i16, iv, cp4, cp16},
/* f4 */ {f4, f4, f4, f8, f4, f4, f4, f4, iv, f4, iv, bf, f4, f4, f4, f4, f4, f4, f4, iv, cp4, cp16},
/* f8 */ {f8, f8, f8, f8, f8, f8, f8, f8, iv, f8, iv, bf, f8, f8, f8, f8, f8, f8, f8, iv, cp4, cp16},
/* i1 */ {i1, i1, f4, f8, i1, i4, i8, i2, iv, f2, iv, bf, i1, i4, i8, i16, iv, i2, i16, iv, cp4, cp16},
/* i4 */ {i4, i4, f4, f8, i4, i4, i8, i4, iv, f2, iv, bf, i4, i4, i8, i16, iv, i4, i16, iv, cp4, cp16},
/* i8 */ {i8, i8, f4, f8, i8, i8, i8, i8, iv, f2, iv, bf, i8, i8, i8, i16, iv, i8, i16, iv, cp4, cp16},
/* u1 */ {c1, c1, f4, f8, i2, i4, i8, u1, iv, f2, iv, bf, u1, u2, u4, u8, u16, i2, i16, iv, cp4, cp16},
/* re */ {iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv},
/* f2 */ {f2, f2, f4, f8, f2, f2, f2, f2, iv, f2, iv, bf, f2, f2, f2, f2, iv, f2, f2, iv, cp4, cp16},
/* bu */ {iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, bu, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv},
/* bf */ {bf, bf, bf, bf, bf, bf, bf, bf, iv, bf, iv, bf, bf, bf, bf, bf, iv, bf, bf, iv, cp4, cp16},
/* b1 */ {c1, c1, f4, f8, i1, i4, i8, u1, iv, f2, iv, bf, b1, u2, u4, u8, u16, i2, i16, iv, cp4, cp16},
/* u2 */ {u2, u2, f4, f8, i4, i4, i8, u2, iv, f2, iv, bf, u2, u2, u4, u8, u16, i4, i16, iv, cp4, cp16},
/* u4 */ {u4, u4, f4, f8, i8, i8, i8, u4, iv, f2, iv, bf, u4, u4, u4, u8, u16, i8, i16, iv, cp4, cp16},
/* u8 */ {u8, u8, f4, f8, i16, i16, i16, u8, iv, f2, iv, bf, u8, u8, u8, u8, u16, i16, i16, iv, cp4, cp16},
/* u16 */ {u16, u16, f4, f8, iv, iv, iv, u16, iv, f2, iv, bf, u16, u16, u16, u16, u16, iv, iv, iv, cp4, cp16},
/* i2 */ {i2, i2, f4, f8, i2, i4, i8, i2, iv, f2, iv, bf, i2, i4, i8, i16, iv, i2, i16, iv, cp4, cp16},
/* i16 */ {i16, i16, f4, f8, i16, i16, i16, i16, iv, f2, iv, bf, i16, i16, i16, i16, iv, i16, i16, iv, cp4, cp16},
/* cp4 */ {iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, iv, cp4, cp8, cp16},
/* cp8 */ {cp8, cp8, cp8, cp8, cp8, cp8, cp8, cp8, iv, cp8, iv, cp8, cp8, cp8, cp8, cp8, cp8, cp8, cp8, cp8, cp8, cp16},
/* cp16 */ {cp16,cp16,cp16,cp16,cp16,cp16,cp16,cp16,iv, cp16,iv, cp16,cp16,cp16,cp16,cp16,cp16, cp16,cp16, cp16, cp16, cp16}};
// clang-format on
return _promoteTypesLookup[static_cast<int>(a->data_type())][static_cast<int>(b->data_type())];
}

Expand Down
13 changes: 11 additions & 2 deletions oneflow/core/framework/dtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ namespace oneflow {
OF_PP_MAKE_TUPLE_SEQ(UInt8) \
OF_PP_MAKE_TUPLE_SEQ(OFRecord) \
OF_PP_MAKE_TUPLE_SEQ(TensorBuffer) \
OF_PP_MAKE_TUPLE_SEQ(BFloat16)
OF_PP_MAKE_TUPLE_SEQ(BFloat16) \
OF_PP_MAKE_TUPLE_SEQ(UInt16) \
OF_PP_MAKE_TUPLE_SEQ(UInt32) \
OF_PP_MAKE_TUPLE_SEQ(UInt64) \
OF_PP_MAKE_TUPLE_SEQ(UInt128) \
OF_PP_MAKE_TUPLE_SEQ(Int16) \
OF_PP_MAKE_TUPLE_SEQ(Int128) \
OF_PP_MAKE_TUPLE_SEQ(Complex32) \
OF_PP_MAKE_TUPLE_SEQ(Complex64) \
OF_PP_MAKE_TUPLE_SEQ(Complex128)

class DType final {
public:
Expand All @@ -55,7 +64,7 @@ class DType final {
Maybe<size_t> bytes() const;

static Maybe<const Symbol<DType>&> Get(DataType);
static const int priority_order[DataType::kMaxDataType];
static const int priority_order[DataType_ARRAYSIZE];

#define DECLARE_GET_DATA_TYPE_FUNCTION(data_type) static const Symbol<DType>& data_type();
OF_PP_FOR_EACH_TUPLE(DECLARE_GET_DATA_TYPE_FUNCTION, DTYPE_SEQ)
Expand Down
9 changes: 9 additions & 0 deletions python/oneflow/compatible/single_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
locals()["record"] = oneflow._oneflow_internal.record
locals()["tensor_buffer"] = oneflow._oneflow_internal.tensor_buffer
locals()["bfloat16"] = oneflow._oneflow_internal.bfloat16
locals()["uint16"] = oneflow._oneflow_internal.uint16
locals()["uint32"] = oneflow._oneflow_internal.uint32
locals()["uint64"] = oneflow._oneflow_internal.uint64
locals()["uint128"] = oneflow._oneflow_internal.uint128
locals()["int16"] = oneflow._oneflow_internal.int16
locals()["int128"] = oneflow._oneflow_internal.int128
locals()["complex32"] = oneflow._oneflow_internal.complex32
locals()["complex64"] = oneflow._oneflow_internal.complex64
locals()["complex128"] = oneflow._oneflow_internal.complex128
from oneflow.compatible.single_client.framework import (
env_util,
session_context,
Expand Down
17 changes: 17 additions & 0 deletions python/oneflow/compatible/single_client/framework/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
flow.record,
flow.tensor_buffer,
flow.bfloat16,
flow.uint16,
flow.uint32,
flow.uint64,
flow.uint128,
flow.int16,
flow.int64,
flow.int128,
flow.complex32,
flow.complex64,
flow.complex128,
]


Expand All @@ -57,6 +67,13 @@ def convert_proto_dtype_to_oneflow_dtype(proto_dtype):
flow.int32: np.int32,
flow.int64: np.int64,
flow.uint8: np.uint8,
flow.uint16: np.uint16,
flow.uint32: np.uint32,
flow.uint64: np.uint64,
flow.int16: np.int16,
flow.int64: np.int64,
flow.complex64: np.complex64,
flow.complex128: np.complex128,
}


Expand Down