mirror of
https://github.com/opencv/opencv.git
synced 2026-12-03 05:33:03 -06:00
Merge pull request #29360 from abhishek-gola:exotic_cast_operations
Support ONNX Cast/CastLike for FP8/FP4/INT4/UINT4/E8M0 dtypes - #29360 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
@@ -552,6 +552,29 @@ Ptr<Graph> Net::Impl::newGraph(const std::string& name_, const std::vector<Arg>&
|
||||
return graph;
|
||||
}
|
||||
|
||||
// No half kernels yet, so half constants are widened just as setGraphInput() widens inputs.
|
||||
void Net::Impl::widenHalfConstants()
|
||||
{
|
||||
if (enableFP16)
|
||||
return;
|
||||
size_t nargs = args.size();
|
||||
__tensors__.resize(nargs);
|
||||
for (size_t i = 1; i < nargs; i++) {
|
||||
ArgData& adata = args[i];
|
||||
if (adata.kind != DNN_ARG_CONST ||
|
||||
(adata.type != CV_16F && adata.type != CV_16BF))
|
||||
continue;
|
||||
Mat& t = __tensors__[i];
|
||||
if (!t.empty()) {
|
||||
Mat widened;
|
||||
widened.fit(t.shape(), accuracy);
|
||||
t.convertTo(widened, accuracy);
|
||||
t = widened;
|
||||
}
|
||||
adata.type = accuracy;
|
||||
}
|
||||
}
|
||||
|
||||
void Net::Impl::prepareForInference()
|
||||
{
|
||||
#ifdef HAVE_ONNXRUNTIME
|
||||
@@ -563,6 +586,7 @@ void Net::Impl::prepareForInference()
|
||||
#endif
|
||||
|
||||
if (!prepared) {
|
||||
widenHalfConstants();
|
||||
fuseQDQ();
|
||||
constFold();
|
||||
fuseBN();
|
||||
@@ -1266,11 +1290,10 @@ void Net::Impl::setGraphInput(Ptr<Graph>& graph, size_t idx, const Mat& m)
|
||||
if ((adata_type == CV_16F || adata_type == CV_16BF) && !enableFP16)
|
||||
adata_type = CV_32F;
|
||||
|
||||
if (adata_type != mtype &&
|
||||
!((adata_type == CV_64F || adata_type == CV_32F || adata_type == CV_16F || adata_type == CV_16BF) &&
|
||||
(mtype == CV_64F || mtype == CV_32F || mtype == CV_16F || mtype == CV_16BF)) &&
|
||||
!((adata_type == CV_8U || adata_type == CV_8S || adata_type == CV_16U || adata_type == CV_16S || adata_type == CV_32S || adata_type == CV_32U || adata_type == CV_64S || adata_type == CV_64U) &&
|
||||
(mtype == CV_8U || mtype == CV_8S || mtype == CV_16U || mtype == CV_16S || mtype == CV_32S || mtype == CV_32U || mtype == CV_64S || mtype == CV_64U)) &&
|
||||
// setInput converts to the declared type, so any numeric source type is acceptable.
|
||||
const bool aNumeric = CV_IS_INT_TYPE(adata_type) || CV_IS_FLOAT_TYPE(adata_type);
|
||||
const bool mNumeric = CV_IS_INT_TYPE(mtype) || CV_IS_FLOAT_TYPE(mtype);
|
||||
if (adata_type != mtype && !(aNumeric && mNumeric) &&
|
||||
!(adata.type == CV_16BF && mtype == CV_16U) && !(adata.type == CV_16F && mtype == CV_16U) &&
|
||||
!m.empty())
|
||||
{
|
||||
@@ -1868,6 +1891,18 @@ void Net::Impl::forwardGraph(Ptr<Graph>& graph, InputArrayOfArrays inputs_,
|
||||
outputsVec[i].fit(outm.shape(), outm.type());
|
||||
outm.copyTo(outputsVec[i]);
|
||||
}
|
||||
// Narrow to the declared output dtype when an op computed in a wider type.
|
||||
// Half is excepted: the graph was widened, so narrowing would only lose precision.
|
||||
int declaredOutType = i < mainGraphOutTypes.size() ? mainGraphOutTypes[i] : -1;
|
||||
if (!enableFP16 && (declaredOutType == CV_16F || declaredOutType == CV_16BF))
|
||||
declaredOutType = -1;
|
||||
if (declaredOutType >= 0 && !outputsVec[i].empty() &&
|
||||
outputsVec[i].depth() != CV_MAT_DEPTH(declaredOutType))
|
||||
{
|
||||
Mat tmp;
|
||||
outputsVec[i].convertTo(tmp, CV_MAT_DEPTH(declaredOutType));
|
||||
outputsVec[i] = tmp;
|
||||
}
|
||||
} else {
|
||||
outputsVec[i] = outm;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user