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 half-precision datatype support #524

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion ngraph_bridge/ngraph_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ static Status TensorDataToVector(const Tensor& tensor, std::vector<T>* vector) {
// Else we have to convert.
else {
switch (dt) {
case DT_HALF:
ConvertTensorDataToVector<Eigen::half, T>(tensor, vector);
break;
case DT_FLOAT:
ConvertTensorDataToVector<float, T>(tensor, vector);
break;
Expand Down Expand Up @@ -350,7 +353,9 @@ Builder::TF_NGRAPH_CONST_MAP() {
{DataType::DT_UINT16,
make_pair(MakeConstOp<uint16>, ng::element::u16)},
{DataType::DT_BOOL,
make_pair(MakeConstOp<bool, char>, ng::element::boolean)}};
make_pair(MakeConstOp<bool, char>, ng::element::boolean)},
{DataType::DT_HALF,
make_pair(MakeConstOp<float, ng::float16>, ng::element::f16)}};
return the_map;
}

Expand Down
3 changes: 1 addition & 2 deletions ngraph_bridge/ngraph_mark_for_clustering.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ static Status TypeConstraintOk(Node* node,
for (const auto& name_and_set : itr->second) {
auto& type_attr_name = name_and_set.first;
auto& allowed_types = name_and_set.second;

DataType dt;

if (GetNodeAttr(node->attrs(), type_attr_name, &dt) != Status::OK() ||
std::find(allowed_types.begin(), allowed_types.end(), dt) ==
allowed_types.end()) {
Expand Down Expand Up @@ -566,6 +564,7 @@ const TypeConstraintMap& GetTypeConstraintMap() {
type_constraint_map["NonMaxSuppressionV4"]["T"] = {
DT_FLOAT}; // TF allows half too
type_constraint_map["OneHot"]["T"] = NGraphDTypes();
type_constraint_map["OneHot"]["TI"] = NGraphIndexDTypes();
type_constraint_map["Pack"]["T"] = NGraphDTypes();
type_constraint_map["Pad"]["T"] = NGraphDTypes();
type_constraint_map["Pad"]["Tpaddings"] = NGraphIndexDTypes();
Expand Down
17 changes: 9 additions & 8 deletions ngraph_bridge/ngraph_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,23 +293,23 @@ void print_node_histogram(const std::unordered_map<string, int>& histogram,

const gtl::ArraySlice<DataType>& NGraphDTypes() {
static gtl::ArraySlice<DataType> result{
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32,
DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, DT_UINT64,
DT_BOOL, DT_QINT8, DT_QUINT8, DT_BFLOAT16};
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32,
DT_INT64, DT_UINT8, DT_UINT16, DT_UINT32, DT_UINT64,
DT_BOOL, DT_QINT8, DT_QUINT8, DT_BFLOAT16, DT_HALF};
return result;
}

const gtl::ArraySlice<DataType>& NGraphNumericDTypes() {
static gtl::ArraySlice<DataType> result{
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32, DT_INT64,
DT_UINT8, DT_UINT16, DT_UINT32, DT_UINT64, DT_BFLOAT16};
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32, DT_INT64,
DT_UINT8, DT_UINT16, DT_UINT32, DT_UINT64, DT_BFLOAT16, DT_HALF};
return result;
}

const gtl::ArraySlice<DataType>& NGraphNumericAndQuantizedDTypes() {
static gtl::ArraySlice<DataType> result{
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32, DT_INT64,
DT_UINT8, DT_UINT16, DT_UINT32, DT_UINT64, DT_QINT8, DT_QUINT8};
DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32, DT_INT64, DT_UINT8,
DT_UINT16, DT_UINT32, DT_UINT64, DT_QINT8, DT_QUINT8, DT_HALF};
return result;
}

Expand All @@ -330,7 +330,8 @@ const gtl::ArraySlice<DataType>& NGraphSupportedQuantizedDTypes() {
}

const gtl::ArraySlice<DataType>& NGraphRealDTypes() {
static gtl::ArraySlice<DataType> result{DT_FLOAT, DT_DOUBLE, DT_BFLOAT16};
static gtl::ArraySlice<DataType> result{DT_FLOAT, DT_DOUBLE, DT_BFLOAT16,
DT_HALF};
return result;
}

Expand Down
18 changes: 12 additions & 6 deletions ngraph_bridge/ngraph_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ Status ValuesFromConstNode(const NodeDef& node,
return errors::InvalidArgument("Node not a Const");
}

if (node.attr().at("dtype").type() != DataTypeToEnum<T>::value) {
auto dt = node.attr().at("dtype").type();
if (node.attr().count("dtype") == 0 || node.attr().count("value") == 0 ||
(dt != DT_HALF && dt != DataTypeToEnum<T>::value)) {
std::stringstream ss;
ss << "Invalid data type defined for Const. Defined: "
<< node.attr().at("dtype").type();
Expand Down Expand Up @@ -151,25 +153,29 @@ Status ValuesFromConstNode(const NodeDef& node,
switch (dt) {
// TODO(amprocte/NGRAPH-2502): there are more element types to support
// here
case DT_HALF:
val_size = tensor.half_val_size();
if (val_size > 0) val_i = static_cast<T>(tensor.half_val()[i]);
break;
case DT_INT32:
val_size = tensor.int_val_size();
if (val_size > 0) val_i = tensor.int_val()[i];
if (val_size > 0) val_i = static_cast<T>(tensor.int_val()[i]);
break;
case DT_INT64:
val_size = tensor.int64_val_size();
if (val_size > 0) val_i = tensor.int64_val()[i];
if (val_size > 0) val_i = static_cast<T>(tensor.int64_val()[i]);
break;
case DT_FLOAT:
val_size = tensor.float_val_size();
if (val_size > 0) val_i = tensor.float_val()[i];
if (val_size > 0) val_i = static_cast<T>(tensor.float_val()[i]);
break;
case DT_BOOL:
val_size = tensor.bool_val_size();
if (val_size > 0) val_i = tensor.bool_val()[i];
if (val_size > 0) val_i = static_cast<T>(tensor.bool_val()[i]);
break;
case DT_DOUBLE:
val_size = tensor.double_val_size();
if (val_size > 0) val_i = tensor.double_val()[i];
if (val_size > 0) val_i = static_cast<T>(tensor.double_val()[i]);
break;
default:
NGRAPH_VLOG(0)
Expand Down