support for fp16 in GetClipMinMax() (#17967)

add support for fp16 in GetClipMinMax()
This commit is contained in:
Guenther Schmuelling 2023-10-17 16:43:30 -07:00 committed by GitHub
parent 3456831413
commit 2ef7abf1ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,38 +32,53 @@ bool GetClipMinMax(const InitializedTensorSet& initializers, const Node& node,
if (!GetType(*node.InputDefs()[0], input_type, logger))
return false;
if (input_type != ONNX_NAMESPACE::TensorProto_DataType_FLOAT) {
LOGS(logger, VERBOSE) << "GetClipMinMax() only support Clip node with float inputs for now. "
<< "The node [" << node_name << "] has input 0 type: " << input_type;
return false;
}
min = std::numeric_limits<float>::lowest();
max = std::numeric_limits<float>::max();
if (node.SinceVersion() < 11) { // Clip opset 1, 6 is using attributes for min/max
NodeAttrHelper helper(node);
// attributes will be always float
min = helper.Get("min", std::numeric_limits<float>::lowest());
max = helper.Get("max", std::numeric_limits<float>::max());
} else {
if (node.InputDefs().size() > 1) { // we have input min
if (node.InputDefs().size() > 1) {
// we have input min
const auto& min_name = node.InputDefs()[1]->Name();
if (!Contains(initializers, min_name)) {
LOGS(logger, VERBOSE) << "Input min of Clip must be known";
return false;
}
Initializer unpacked_tensor(*initializers.at(min_name));
min = unpacked_tensor.DataAsSpan<float>()[0];
}
if (node.InputDefs().size() > 2) { // we have input max
const auto& max_name = node.InputDefs()[2]->Name();
if (!Contains(initializers, max_name)) {
LOGS(logger, VERBOSE) << "Input max of Clip must be known";
return false;
Initializer unpacked_tensor_min(*initializers.at(min_name));
switch (input_type) {
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT:
min = unpacked_tensor_min.DataAsSpan<float>()[0];
break;
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16:
min = (unpacked_tensor_min.DataAsSpan<MLFloat16>()[0]).ToFloat();
break;
default:
LOGS(logger, VERBOSE) << "GetClipMinMax() only support Clip node with float inputs for now. "
<< "The node [" << node_name << "] has input 0 type: " << input_type;
return false;
}
if (node.InputDefs().size() > 2) {
// we have input max
const auto& max_name = node.InputDefs()[2]->Name();
if (!Contains(initializers, max_name)) {
LOGS(logger, VERBOSE) << "Input max of Clip must be known";
return false;
}
Initializer unpacked_tensor_max(*initializers.at(max_name));
switch (input_type) {
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT:
max = unpacked_tensor_max.DataAsSpan<float>()[0];
break;
case ONNX_NAMESPACE::TensorProto_DataType_FLOAT16:
max = (unpacked_tensor_max.DataAsSpan<MLFloat16>()[0]).ToFloat();
break;
}
}
Initializer unpacked_tensor(*initializers.at(max_name));
max = unpacked_tensor.DataAsSpan<float>()[0];
}
}