Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
aidge_core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noam Zerah
aidge_core
Commits
96f1bbd8
Commit
96f1bbd8
authored
5 months ago
by
Noam Zerah
Browse files
Options
Downloads
Patches
Plain Diff
Adding Utils functions to manipulate Metaoperator ScalingMeta and MulPTQ
parent
6edce726
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/aidge/operator/MetaOperatorDefs.hpp
+8
-5
8 additions, 5 deletions
include/aidge/operator/MetaOperatorDefs.hpp
src/operator/MetaOperatorDefs/Scaling.cpp
+90
-58
90 additions, 58 deletions
src/operator/MetaOperatorDefs/Scaling.cpp
with
98 additions
and
63 deletions
include/aidge/operator/MetaOperatorDefs.hpp
+
8
−
5
View file @
96f1bbd8
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
#include
<array>
#include
<array>
#include
<memory>
#include
<memory>
#include
<string>
#include
<string>
#include
<utility>
#include
"aidge/graph/GraphView.hpp"
#include
"aidge/graph/GraphView.hpp"
#include
"aidge/graph/Node.hpp"
#include
"aidge/graph/Node.hpp"
...
@@ -165,16 +166,18 @@ std::shared_ptr<Node> LSTM(DimSize_t in_channels,
...
@@ -165,16 +166,18 @@ std::shared_ptr<Node> LSTM(DimSize_t in_channels,
std
::
shared_ptr
<
MetaOperator_Op
>
LSTM_Op
(
DimSize_t
seq_length
);
std
::
shared_ptr
<
MetaOperator_Op
>
LSTM_Op
(
DimSize_t
seq_length
);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
bool
UpdateMulPTQ_SF
(
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQNode
,
float
newScalingFactor
);
float
GetMulPTQ_SF
(
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQNode
);
std
::
shared_ptr
<
Node
>
ScalingMeta
(
float
scalingFactor
,
std
::
size_t
nbBits
,
bool
isOutputUnsigned
,
const
std
::
string
&
name
=
""
);
//Helper Function to retrieve and update SF from MulPTQ node
//May be useful to
bool
UpdateScalingNode_ScalingFactor
(
std
::
shared_ptr
<
Aidge
::
Node
>
Node
,
float
newScalingFactor
);
float
GetScalingNode_Scaling_Factor
(
std
::
shared_ptr
<
Aidge
::
Node
>
Node
);
bool
setScalingMetaClipRange
(
std
::
shared_ptr
<
Aidge
::
Node
>
ScalingMetaNode
,
float
min
,
float
max
);
std
::
pair
<
float
&
,
float
&>
getMinMaxRefClip
(
std
::
shared_ptr
<
Aidge
::
Node
>
ScalingMetaNode
);
std
::
shared_ptr
<
MetaOperator_Op
>
ScalingMeta
_Op
(
float
scalingFactor
,
std
::
size_t
nbBits
,
bool
isOutputUnsigned
);
std
::
shared_ptr
<
Aidge
::
Node
>
ScalingMeta
(
float
scalingFactor
,
float
clip_min
,
float
clip_max
,
const
std
::
string
&
name
);
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQ
(
float
scalingFactor
,
const
std
::
string
&
name
=
""
);
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQ
(
float
scalingFactor
,
const
std
::
string
&
name
=
""
);
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
MulPTQ_Op
(
float
scalingFactor
);
}
// namespace Aidge
}
// namespace Aidge
...
...
This diff is collapsed.
Click to expand it.
src/operator/MetaOperatorDefs/Scaling.cpp
+
90
−
58
View file @
96f1bbd8
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
#include
<array>
#include
<array>
#include
<memory>
#include
<memory>
#include
<utility>
//Operator
//Operator
#include
"aidge/operator/Clip.hpp"
#include
"aidge/operator/Clip.hpp"
...
@@ -27,92 +28,123 @@
...
@@ -27,92 +28,123 @@
#include
"aidge/utils/ArrayHelpers.hpp"
#include
"aidge/utils/ArrayHelpers.hpp"
#include
"aidge/utils/Types.h"
#include
"aidge/utils/Types.h"
#include
"aidge/operator/Identity.hpp"
#include
"aidge/operator/Identity.hpp"
//Legacy Functions getter/set for Scaling Factor
bool
Aidge
::
Update
MulPTQ_SF
(
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQ
Node
,
float
newScalingFactor
)
bool
Aidge
::
Update
ScalingNode_ScalingFactor
(
std
::
shared_ptr
<
Aidge
::
Node
>
Node
,
float
newScalingFactor
)
{
{
if
(
MulPTQNode
->
type
()
!=
"
MulPTQ
"
)
if
(
Node
->
type
()
!=
"
MulPTQ
"
&&
Node
->
type
()
!=
"
ScalingMeta
"
)
{
{
Log
::
notice
(
"Cannot use UpdateMulPTQ_SF on Node of type {}"
,
MulPTQNode
->
type
());
AIDGE_ASSERT
(
"Cannot use UpdateMulPTQ_SF on Node of type {}"
,
Node
->
type
());
return
false
;
}
}
std
::
shared_ptr
<
Tensor
>
newScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
newScalingFactor
});
std
::
shared_ptr
<
Tensor
>
newScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
newScalingFactor
});
MulPTQNode
->
input
(
1
).
first
->
getOperator
()
->
setOutput
(
0
,
newScalingFactorTensorAttached
);
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
MetaOp
=
std
::
static_pointer_cast
<
MetaOperator_Op
>
(
Node
->
getOperator
());
return
true
;
std
::
set
<
Aidge
::
NodePtr
>
Meta_Op_Node_List
=
MetaOp
->
getMicroGraph
()
->
getNodes
();
//List of Node inside MulPTQ Metaop Node
for
(
std
::
shared_ptr
<
Aidge
::
Node
>
node
:
Meta_Op_Node_List
)
{
if
(
node
->
type
()
==
"Mul"
)
{
node
->
input
(
1
).
first
->
getOperator
()
->
setOutput
(
0
,
newScalingFactorTensorAttached
);
return
true
;
}
}
AIDGE_ASSERT
(
"Invalid MetaOperator MulPTQ, no Mul node found inside node of type {}"
,
Node
->
type
());
return
false
;
}
}
float
Aidge
::
GetMulPTQ_SF
(
std
::
shared_ptr
<
Aidge
::
Node
>
MulPTQNode
)
float
Aidge
::
GetScalingNode_Scaling_Factor
(
std
::
shared_ptr
<
Aidge
::
Node
>
Node
)
{
{
if
(
MulPTQNode
->
type
()
!=
"
MulPTQ
"
)
if
(
Node
->
type
()
!=
"
MulPTQ
"
&&
Node
->
type
()
!=
"
ScalingMeta
"
)
{
{
Log
::
notice
(
"Cannot use Get
MulPTQ_SF
on Node of type {}"
,
MulPTQ
Node
->
type
());
AIDGE_ASSERT
(
"Cannot use Get
ScalingNode_Scaling_Factor
on Node of type {}"
,
Node
->
type
());
return
-
1
;
return
-
1
;
}
}
auto
t
=
MulPTQNode
->
input
(
1
).
first
->
getOperator
()
->
getRawOutput
(
0
);
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
MetaOp
=
std
::
static_pointer_cast
<
MetaOperator_Op
>
(
Node
->
getOperator
());
auto
v
=
std
::
static_pointer_cast
<
Tensor
>
(
t
)
->
getImpl
()
->
rawPtr
();
std
::
set
<
Aidge
::
NodePtr
>
Meta_Op_Node_List
=
MetaOp
->
getMicroGraph
()
->
getNodes
();
//List of Node inside MulPTQ Metaop Node
return
*
(
static_cast
<
float
*>
(
v
));
for
(
std
::
shared_ptr
<
Aidge
::
Node
>
node
:
Meta_Op_Node_List
)
{
if
(
node
->
type
()
==
"Mul"
)
{
std
::
shared_ptr
<
Aidge
::
Data
>
MulInput1Data
=
node
->
input
(
1
).
first
->
getOperator
()
->
getRawOutput
(
0
);
void
*
RawInputScalingFactor
=
std
::
static_pointer_cast
<
Tensor
>
(
MulInput1Data
)
->
getImpl
()
->
rawPtr
();
return
(
*
(
static_cast
<
float
*>
(
RawInputScalingFactor
)));
}
}
AIDGE_ASSERT
(
"Invalid MetaOperator Node, no Mul node found inside node of type {}"
,
Node
->
type
());
return
-
1
;
}
}
/*
std
::
shared_ptr
<
Aidge
::
Node
>
Aidge
::
ScalingMeta
(
float
scalingFactor
,
std
::
size_t
nbBits
,
bool
isOutputUnsigned
,
const
std
::
string
&
nam
e
)
std::
pair<float&,float&> Aidge::getMinMaxRefClip(std::shared_ptr<Aidge::Node> ScalingMetaNod
e)
{
{
if(ScalingMetaNode->type() != "ScalingMeta")
{
AIDGE_ASSERT("Cannot use getMinMaxRefClip on Node of type {}",ScalingMetaNode->type());
}
std::shared_ptr<Aidge::MetaOperator_Op> MetaOp = std::static_pointer_cast<MetaOperator_Op>(ScalingMetaNode->getOperator());
std::set<Aidge::NodePtr> Meta_Op_Node_List = MetaOp->getMicroGraph()->getNodes(); //List of Node inside MulPTQ Metaop Node
for(std::shared_ptr<Aidge::Node> node : Meta_Op_Node_List)
{
if(node->type() == "Clip")
{
std::shared_ptr<Clip_Op> Clip_Node_Op = std::static_pointer_cast<Clip_Op>(node->getOperator());
return std::make_pair(std::ref(Clip_Node_Op->min()),std::ref(Clip_Node_Op->max()));
}
}
AIDGE_ASSERT("Invalid MetaOperator ScalingMeta, no Clip node found inside Node of type {}",ScalingMetaNode->type());
}*/
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
bool
Aidge
::
setScalingMetaClipRange
(
std
::
shared_ptr
<
Aidge
::
Node
>
ScalingMetaNode
,
float
min
,
float
max
)
{
float
minv
=
isOutputUnsigned
?
0
:
if
(
ScalingMetaNode
->
type
()
!=
"ScalingMeta"
)
-
(
1ll
<<
(
nbBits
-
1ll
));
{
float
maxv
=
isOutputUnsigned
?
(
1ll
<<
nbBits
)
-
1ll
:
AIDGE_ASSERT
(
"Cannot use setScalingMetaClipRange on Node of type {}"
,
ScalingMetaNode
->
type
());
(
1ll
<<
(
nbBits
-
1ll
))
-
1ll
;
return
false
;
auto
mul_node
=
Mul
((
!
name
.
empty
())
?
name
+
"_MulPTQ"
:
""
);
}
addProducer
(
mul_node
,
1
,{
1
},
"ScalingFactor"
);
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
MetaOp
=
std
::
static_pointer_cast
<
MetaOperator_Op
>
(
ScalingMetaNode
->
getOperator
());
mul_node
->
input
(
1
).
first
->
getOperator
()
->
setOutput
(
0
,
ScalingFactorTensorAttached
);
std
::
set
<
Aidge
::
NodePtr
>
Meta_Op_Node_List
=
MetaOp
->
getMicroGraph
()
->
getNodes
();
//List of Node inside MulPTQ Metaop Node
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
for
(
std
::
shared_ptr
<
Aidge
::
Node
>
node
:
Meta_Op_Node_List
)
mul_node
,
{
Clip
((
!
name
.
empty
())
?
name
+
"_ClipPTQ"
:
""
,
minv
,
maxv
),
if
(
node
->
type
()
==
"Clip"
)
Round
((
!
name
.
empty
())
?
name
+
"_RoundPTQ"
:
""
)});
{
auto
connectedGV
=
getConnectedGraphView
(
mul_node
);
std
::
shared_ptr
<
Clip_Op
>
Clip_Node_Op
=
std
::
static_pointer_cast
<
Clip_Op
>
(
node
->
getOperator
());
std
::
shared_ptr
<
Aidge
::
Node
>
metaopNode
=
MetaOperator
(
"ScalingMeta"
,
connectedGV
);
Clip_Node_Op
->
max
()
=
max
;
return
metaopNode
;
Clip_Node_Op
->
min
()
=
min
;
return
true
;
}
}
AIDGE_ASSERT
(
"Invalid MetaOperator MulPTQ, no Mul node found inside Node of type {}"
,
ScalingMetaNode
->
type
());
return
false
;
}
}
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
Aidge
::
ScalingMeta
_Op
(
float
scalingFactor
,
std
::
size_t
nbBits
,
bool
isOutputUnsigned
)
std
::
shared_ptr
<
Aidge
::
Node
>
Aidge
::
ScalingMeta
(
float
scalingFactor
,
float
clip_min
,
float
clip_max
,
const
std
::
string
&
name
)
{
{
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
std
::
shared_ptr
<
Aidge
::
Node
>
mul_node
=
Mul
((
!
name
.
empty
())
?
name
+
"_MulPTQ"
:
""
);
float
minv
=
isOutputUnsigned
?
0
:
std
::
shared_ptr
<
Aidge
::
Node
>
producer_scaling_factor
=
addProducer
(
mul_node
,
1
,{
1
},
"ScalingFactor"
);
-
(
1ll
<<
(
nbBits
-
1ll
));
producer_scaling_factor
->
getOperator
()
->
setOutput
(
0
,
ScalingFactorTensorAttached
);
float
maxv
=
isOutputUnsigned
?
(
1ll
<<
nbBits
)
-
1ll
:
(
1ll
<<
(
nbBits
-
1ll
))
-
1ll
;
std
::
shared_ptr
<
Aidge
::
Node
>
mul
=
Mul
();
mul
->
getOperator
()
->
associateInput
(
1
,
ScalingFactorTensorAttached
);
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
mul
,
mul_node
,
Clip
(
""
,
minv
,
maxv
),
Clip
((
!
name
.
empty
())
?
name
+
"_ClipPTQ"
:
""
,
clip_min
,
clip_max
),
Round
()});
Round
((
!
name
.
empty
())
?
name
+
"_RoundPTQ"
:
""
)});
return
std
::
make_shared
<
MetaOperator_Op
>
(
"ScalingMeta"
,
graph
);
std
::
shared_ptr
<
Aidge
::
GraphView
>
connectedGV
=
getConnectedGraphView
(
mul_node
);
std
::
shared_ptr
<
Aidge
::
Node
>
metaopNode
=
MetaOperator
(
"ScalingMeta"
,
connectedGV
);
return
metaopNode
;
}
}
// The only purpose of MulPTQ is to encapsulate the Mul operator in order to tag it (as a PTQ node and not an ordinary Mul).
// The only purpose of MulPTQ is to encapsulate the Mul operator in order to tag it (as a PTQ node and not an ordinary Mul).
std
::
shared_ptr
<
Aidge
::
Node
>
Aidge
::
MulPTQ
(
float
scalingFactor
,
const
std
::
string
&
name
)
std
::
shared_ptr
<
Aidge
::
Node
>
Aidge
::
MulPTQ
(
float
scalingFactor
,
const
std
::
string
&
name
)
{
{
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
auto
mul_node
=
Mul
((
!
name
.
empty
())
?
name
+
"_MulPTQ"
:
""
);
addProducer
(
mul_node
,
1
,{
1
},
"ScalingFactor"
);
std
::
shared_ptr
<
Aidge
::
Node
>
mul_node
=
Mul
((
!
name
.
empty
())
?
name
+
"_MulPTQ"
:
""
);
mul_node
->
input
(
1
).
first
->
getOperator
()
->
setOutput
(
0
,
ScalingFactorTensorAttached
);
std
::
shared_ptr
<
Aidge
::
Node
>
producer_scaling_factor
=
addProducer
(
mul_node
,
1
,{
1
},
"ScalingFactor"
);
producer_scaling_factor
->
getOperator
()
->
setOutput
(
0
,
ScalingFactorTensorAttached
);
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
mul_node
});
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
mul_node
});
auto
connectedGV
=
getConnectedGraphView
(
mul_node
);
std
::
shared_ptr
<
Aidge
::
GraphView
>
connectedGV
=
getConnectedGraphView
(
mul_node
);
Aidge
::
NodePtr
metaopNode
=
MetaOperator
(
"MulPTQ"
,
connectedGV
);
Aidge
::
NodePtr
metaopNode
=
MetaOperator
(
"MulPTQ"
,
connectedGV
);
return
metaopNode
;
return
metaopNode
;
}
}
std
::
shared_ptr
<
Aidge
::
MetaOperator_Op
>
Aidge
::
MulPTQ_Op
(
float
scalingFactor
)
{
std
::
shared_ptr
<
Tensor
>
ScalingFactorTensorAttached
=
std
::
make_shared
<
Tensor
>
(
Array1D
<
float
,
1
>
{
scalingFactor
});
auto
mul_node
=
Mul
(
""
);
addProducer
(
mul_node
,
1
,{
1
},
"ScalingFactor"
);
mul_node
->
input
(
1
).
first
->
getOperator
()
->
setOutput
(
0
,
ScalingFactorTensorAttached
);
std
::
shared_ptr
<
Aidge
::
GraphView
>
graph
=
Sequential
({
mul_node
});
auto
connectedGV
=
getConnectedGraphView
(
mul_node
);
return
std
::
make_shared
<
MetaOperator_Op
>
(
"MulPTQ_OP"
,
connectedGV
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment