| 1000 | } |
| 1001 | |
| 1002 | bool InputQuery::constructReluLayer( NLR::NetworkLevelReasoner *nlr, |
| 1003 | Map<unsigned, unsigned> &handledVariableToLayer, |
| 1004 | unsigned newLayerIndex, |
| 1005 | Set<PiecewiseLinearConstraint *> &handledPLConstraints ) |
| 1006 | { |
| 1007 | INPUT_QUERY_LOG( "Attempting to construct ReluLayer..." ); |
| 1008 | struct NeuronInformation |
| 1009 | { |
| 1010 | public: |
| 1011 | NeuronInformation( unsigned variable, unsigned neuron, unsigned sourceVariable ) |
| 1012 | : _variable( variable ) |
| 1013 | , _neuron( neuron ) |
| 1014 | , _sourceVariable( sourceVariable ) |
| 1015 | { |
| 1016 | } |
| 1017 | |
| 1018 | unsigned _variable; |
| 1019 | unsigned _neuron; |
| 1020 | unsigned _sourceVariable; |
| 1021 | }; |
| 1022 | |
| 1023 | List<NeuronInformation> newNeurons; |
| 1024 | |
| 1025 | // Look for ReLUs where all b variables have already been handled |
| 1026 | const List<PiecewiseLinearConstraint *> &plConstraints = getPiecewiseLinearConstraints(); |
| 1027 | |
| 1028 | unsigned currentSourceLayer = 0; |
| 1029 | for ( const auto &plc : plConstraints ) |
| 1030 | { |
| 1031 | if ( handledPLConstraints.exists( plc ) ) |
| 1032 | continue; |
| 1033 | |
| 1034 | // Only consider ReLUs |
| 1035 | if ( plc->getType() != RELU ) |
| 1036 | continue; |
| 1037 | |
| 1038 | const ReluConstraint *relu = (const ReluConstraint *)plc; |
| 1039 | |
| 1040 | // Has the b variable been handled? |
| 1041 | unsigned b = relu->getB(); |
| 1042 | if ( !handledVariableToLayer.exists( b ) || |
| 1043 | ( _ensureSameSourceLayerInNLR && !newNeurons.empty() && |
| 1044 | handledVariableToLayer[b] != currentSourceLayer ) ) |
| 1045 | continue; |
| 1046 | |
| 1047 | // If the f variable has also been handled, ignore this constraint |
| 1048 | unsigned f = relu->getF(); |
| 1049 | if ( handledVariableToLayer.exists( f ) ) |
| 1050 | continue; |
| 1051 | |
| 1052 | // B has been handled, f hasn't. Add f |
| 1053 | if ( _ensureSameSourceLayerInNLR && newNeurons.empty() ) |
| 1054 | currentSourceLayer = handledVariableToLayer[b]; |
| 1055 | newNeurons.append( NeuronInformation( f, newNeurons.size(), b ) ); |
| 1056 | nlr->addConstraintInTopologicalOrder( plc ); |
| 1057 | handledPLConstraints.insert( plc ); |
| 1058 | } |
| 1059 |
nothing calls this directly
no test coverage detected