Helpers Overview (CUDNN, OneDNN,Armcompute)

Requirements Helper

Requirements helper was introduced to replace plain checks for making them output informative messages (Debug and Verbose mode) and also replace macros REQUIRE_TRUE.
    it will lazily evaluate values and messages if the type wrapped and hasgetValue and getMsg methods
    it is implicit bool. this makes it usable with logical operators and also inside if conditions. Besides it will benefit from shortcircuit nature of those operators.
    it has the following check methods
    1
    Requirements& expect(const T& expVar,const T1& reqVar, Op comparision, const char *first_half="")
    2
    Requirements& expectEq(const T& exp,const T1& req)
    3
    Requirements& expectNotEq(const T& exp,const T1& req)
    4
    Requirements& expectLess(const T& exp,const T1& req)
    5
    Requirements& expectLessEq(const T& exp,const T1& req)
    6
    Requirements& expectGreater(T exp, T1 req)
    7
    Requirements& expectGreaterEq(const T& exp,const T1& req
    8
    Requirements& expectTrue(const T& expVar, const char *msg=)
    9
    Requirements& expectFalse(const T& expVar, const char *msg=)
    Copied!
    you can either log the success case or throw error on the failure
    it can use plain types for checks.
    if value has stream operator it will be used to output it's value. for custom types you may need add that by yourself
    ostream& operator<<(ostream& os, const CustomUserType& dt)
    there is generic template InfoVariable wrapper for types to make it informative. you can use lambda operators with them as well to make it lazily evaluated
    we added custom ShapeInfoVariable wrapper for the NDArray and vector<> shapes to make them informative
    one can use expect to add its own proper comparision. simple lambda for that will be like this:
    1
    [](const decltype(expType)& l, const decltype(reqType)& r){
    2
    //compare and return
    3
    return ....;
    4
    }
    Copied!

Examples:

firstly, we should enable logging
1
sd::Environment::getInstance().setDebug(true);
2
sd::Environment::getInstance().setVerbose(true);
Copied!
    1.
    simple case
1
Requirements req1("Requirement Helper Example#1");
2
int x = 20;
3
req1.expectLess(x, 22);
4
req1.expectEq(x, 21); //should fail
Copied!
Output: Requirement Helper Example#1: {20} expected to be equal to 21
    1.
    using InfoVariable wrapper
    1
    int age = 15;
    2
    Requirements req2("Requirement Helper Example#2");
    3
    req2.expectGreaterEq(makeInfoVariable(age, "the user's age"), 18);
    Copied!
    Output:
    1
    Requirement Helper Example#2: the user's age {15} expected to be greater than or equal 18
    Copied!
    2.
    helper behavior while using many checks in one block
    1
    int getAge(){
    2
    std::cout<<"getAge() was called"<<std::endl;
    3
    return 15;
    4
    }
    5
    ....
    6
    Requirements req3("Requirement Helper Example#3");
    7
    int z = 20;
    8
    req3.expectEq(z, 21);
    9
    req3.expectGreaterEq(makeInfoVariable(getAge(), "the user's age"), 18);
    Copied!
    Output:
    1
    Requirement Helper Example#3: {20} expected to be equal to 21
    2
    getAge() was called
    Copied!
As it is seen the second check did not happen as the previous failed. But still getAge() method was called as its function argument.
    1.
    using shortcircuit to avoid Requirement call at all if the previous one was failed
    1
    Requirements req4("Requirement Helper Example#4");
    2
    int zz = 20;
    3
    req4.expectEq(zz, 21) && //shortcicuit And
    4
    req4.expectGreaterEq(makeInfoVariable(getAge(), "the user's age"), 18);
    Copied!
    Output:
    1
    Requirement Helper Example#4: {20} expected to be equal to 21
    Copied!
    2.
    using lambdas with InfoVariable. it will make it lazily evaluated
    1
    Requirements req5("Requirement Helper Example#5");
    2
    req5.expectEq( 21,
    3
    makeInfoVariable(21, []{
    4
    std::cout<<"lambda call#1"<<std::endl;
    5
    return "twenty one";
    6
    }));
    7
    req5.expectEq(makeInfoVariable([]{ return 20;}, []{return "twenty";}),
    8
    makeInfoVariable(21, []{
    9
    std::cout<<"lambda call#2"<<std::endl;
    10
    return "twenty one";
    11
    }));
    12
    req5.expectGreaterEq(makeInfoVariable([]{
    13
    std::cout<<"lambda call#3" <<std::endl;
    14
    return 15;
    15
    },
    16
    []{ return "the user's age";}),
    17
    makeInfoVariable([]{return 18;}, []{return "the allowed age";})
    18
    );
    Copied!
    Output:
    ```
    lambda call#2
    Requirement Helper Example#5: twenty {20} expected to be equal to twenty one 21
1
6. use bool nature and also log the success case
2
```cpp
3
Requirements req6("Requirement Helper Example#6");
4
NDArray * arr= nullptr;
5
arr !=nullptr && req6.expectEq(arr->rankOf(), 3) ;
6
req6.logTheSuccess();
Copied!
Output:
1
Requirement Helper Example#6: meets the requirements
Copied!
    1.
    custom comparision lambda and also another usage of the custom wrapper written by us ShapeInfoVariable. Note: we will use std::vector<int>. this wrapper can be used with NDArray as well.
    1
    Requirements req7("Requirement Helper Example#7");
    2
    req7.expect(makeShapeInfoVariable(std::vector<int>{2,3,4,5}, SHAPE_MSG_INPUT0), makeShapeInfoVariable(std::vector<int>{2,3,4,7}, SHAPE_MSG_INPUT1),
    3
    [](const std::vector<int>& l, const std::vector<int>& r){
    4
    if(l.size()!=r.size()) return false;
    5
    for (int i = 0; i < l.size(); i++) {
    6
    if (l.at(i) != r.at(i)) {
    7
    return false;
    8
    }
    9
    }
    10
    return true;
    11
    }
    12
    , EXPECTED_EQ_MSG);
    13
    }
    Copied!
Output:
1
Requirement Helper Example#7: the Shape of the Input NDArray#0 {[2, 3, 4, 5]} expected to be equal to the Shape of the Input NDArray#1 [2, 3, 4, 7]
Copied!
    1.
    throw error when there is failure
    1
    Requirements req8("Requirement Helper Example#8");
    2
    req8.expectEq(6,6) &&
    3
    req8.expectIn(6, {1,2,3,7,8,9});
    4
    req8.throws();
    Copied!
    Output:
    1
    terminate called after throwing an instance of 'std::invalid_argument'
    2
    what(): Op validation failed
    3
    ...
    4
    Requirement Helper Example#8: {6} expected to be one of these {[1, 2, 3, 7, 8, 9, ]}
    Copied!

Here is live example:

Note: some classes were mocked there and do not represent the exact implementations in libnd4j. https://godbolt.org/z/sq98vchs5
Export as PDF
Copy link