Deeplearning4j
Community ForumND4J JavadocDL4J Javadoc
EN 1.0.0-M2
EN 1.0.0-M2
  • Deeplearning4j Suite Overview
  • Release Notes
    • 1.0.0-M2
    • 1.0.0-M1.1
    • 1.0.0-M1
    • 1.0.0-beta7
    • 1.0.0-beta6
    • 1.0.0-beta5
    • 1.0.0-beta4
    • 1.0.0-beta3
    • 1.0.0-beta2
    • 1.0.0-beta
    • 1.0.0-alpha
    • 0.9.1
    • 0.9.0
    • 0.8.0
    • 0.7.2
    • 0.7.1
    • 0.7.0
    • 0.6.0
    • 0.5.0
    • 0.4.0
    • 1.00-M2.2
  • Multi-Project
    • Tutorials
      • Beginners
      • Quickstart
    • How To Guides
      • Import in to your favorite IDE
      • Contribute
        • Eclipse Contributors
      • Developer Docs
        • Github Actions/Build Infra
        • Javacpp
        • Release
        • Testing
      • Build From Source
      • Benchmark
      • Beginners
    • Reference
      • Examples Tour
    • Explanation
      • The core workflow
      • Configuration
        • Backends
          • Performance Issues
          • CPU
          • Cudnn
        • Memory
          • Workspaces
      • Build Tools
      • Snapshots
      • Maven
  • Deeplearning4j
    • Tutorials
      • Quick Start
      • Language Processing
        • Doc2Vec
        • Sentence Iterator
        • Tokenization
        • Vocabulary Cache
    • How To Guides
      • Custom Layers
      • Keras Import
        • Functional Models
        • Sequential Models
        • Custom Layers
        • Keras Import API Overview
          • Advanced Activations
          • Convolutional Layers
          • Core Layers
          • Embedding Layers
          • Local Layers
          • Noise Layers
          • Normalization Layers
          • Pooling Layers
          • Recurrent Layers
          • Wrapper Layers
        • Supported Features Overview
          • Activations
          • Constraints
          • Initializers
          • Losses
          • Optimizers
          • Regularizers
      • Tuning and Training
        • Visualization
        • Troubleshooting Training
        • Early Stopping
        • Evaluation
        • Transfer Learning
    • Reference
      • Model Zoo
        • Zoo Models
      • Activations
      • Auto Encoders
      • Computation Graph
      • Convolutional Layers
      • DataSet Iterators
      • Layers
      • Model Listeners
      • Saving and Loading Models
      • Multi Layer Network
      • Recurrent Layers
      • Updaters/Optimizers
      • Vertices
      • Word2vec/Glove/Doc2Vec
    • Explanation
  • datavec
    • Tutorials
      • Overview
    • How To Guides
    • Reference
      • Analysis
      • Conditions
      • Executors
      • Filters
      • Normalization
      • Operations
      • Transforms
      • Readers
      • Records
      • Reductions
      • Schemas
      • Serialization
      • Visualization
    • Explanation
  • Nd4j
    • Tutorials
      • Quickstart
    • How To Guides
      • Other Framework Interop
        • Tensorflow
        • TVM
        • Onnx
      • Matrix Manipulation
      • Element wise Operations
      • Basics
    • Reference
      • Op Descriptor Format
      • Tensor
      • Syntax
    • Explanation
  • Samediff
    • Tutorials
      • Quickstart
    • How To Guides
      • Importing Tensorflow
      • Adding Operations
        • codegen
    • Reference
      • Operation Namespaces
        • Base Operations
        • Bitwise
        • CNN
        • Image
        • LinAlg
        • Loss
        • Math
        • NN
        • Random
        • RNN
      • Variables
    • Explanation
      • Model Import Framework
  • Libnd4j
    • How To Guides
      • Building on Windows
      • Building for raspberry pi or Jetson Nano
      • Building on ios
      • How to Add Operations
      • How to Setup CLion
    • Reference
      • Understanding graph execution
      • Overview of working with libnd4j
      • Helpers Overview (CUDNN, OneDNN,Armcompute)
    • Explanation
  • Python4j
    • Tutorials
      • Quickstart
    • How To Guides
      • Write Python Script
    • Reference
      • Python Types
      • Python Path
      • Garbage Collection
      • Python Script Execution
    • Explanation
  • Spark
    • Tutorials
      • DL4J on Spark Quickstart
    • How To Guides
      • How To
      • Data How To
    • Reference
      • Parameter Server
      • Technical Reference
    • Explanation
      • Spark API Reference
  • codegen
Powered by GitBook
On this page
  • Requirements Helper
  • Examples:

Was this helpful?

Export as PDF
  1. Libnd4j
  2. Reference

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

    Requirements& expect(const T& expVar,const T1& reqVar, Op comparision, const char *first_half="")
    Requirements& expectEq(const T& exp,const T1& req)
    Requirements& expectNotEq(const T& exp,const T1& req)
    Requirements& expectLess(const T& exp,const T1& req)
    Requirements& expectLessEq(const T& exp,const T1& req)
    Requirements& expectGreater(T exp, T1 req)
    Requirements& expectGreaterEq(const T& exp,const T1& req
    Requirements& expectTrue(const T& expVar, const char *msg=)
    Requirements& expectFalse(const T& expVar, const char *msg=)
  • 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:

    [](const decltype(expType)& l, const decltype(reqType)& r){
              //compare and return
              return  ....;
          }

Examples:

firstly, we should enable logging

    sd::Environment::getInstance().setDebug(true);
    sd::Environment::getInstance().setVerbose(true);
  1. simple case

Requirements req1("Requirement Helper Example#1");
int x = 20;
req1.expectLess(x, 22);
req1.expectEq(x, 21); //should fail

Output: Requirement Helper Example#1: {20} expected to be equal to 21

  1. using InfoVariable wrapper

    int age = 15;
    Requirements req2("Requirement Helper Example#2");
    req2.expectGreaterEq(makeInfoVariable(age, "the user's age"), 18);

    Output:

    Requirement Helper Example#2: the user's age {15} expected to be greater than or equal  18
  2. helper behavior while using many checks in one block

    int getAge(){
     std::cout<<"getAge() was called"<<std::endl;
     return 15;
    }
    ....
    Requirements req3("Requirement Helper Example#3");
    int z = 20;
    req3.expectEq(z, 21); 
    req3.expectGreaterEq(makeInfoVariable(getAge(), "the user's age"), 18);

    Output:

    Requirement Helper Example#3:  {20} expected to be equal to  21
    getAge() was called

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

    Requirements req4("Requirement Helper Example#4");
    int zz = 20;
    req4.expectEq(zz, 21) &&  //shortcicuit And
    req4.expectGreaterEq(makeInfoVariable(getAge(), "the user's age"), 18);

    Output:

    Requirement Helper Example#4:  {20} expected to be equal to  21
  2. using lambdas with InfoVariable. it will make it lazily evaluated

    Requirements req5("Requirement Helper Example#5"); 
    req5.expectEq( 21, 
             makeInfoVariable(21, []{
                    std::cout<<"lambda call#1"<<std::endl;
                    return "twenty one";
                }));
    req5.expectEq(makeInfoVariable([]{ return 20;}, []{return "twenty";}), 
                makeInfoVariable(21, []{
                    std::cout<<"lambda call#2"<<std::endl;
                    return "twenty one";
                }));
    req5.expectGreaterEq(makeInfoVariable([]{
                         std::cout<<"lambda call#3" <<std::endl;
                         return 15;
                         }, 
                         []{ return "the user's age";}), 
                      makeInfoVariable([]{return 18;}, []{return "the allowed age";})
       );

    Output:

    ```

    lambda call#2

    Requirement Helper Example#5: twenty {20} expected to be equal to twenty one 21

6. use bool nature and also log the success case
```cpp
Requirements req6("Requirement Helper Example#6");
NDArray * arr= nullptr;
arr !=nullptr && req6.expectEq(arr->rankOf(), 3) ;
req6.logTheSuccess();

Output:

Requirement Helper Example#6: meets the requirements
  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.

    Requirements req7("Requirement Helper Example#7");
    req7.expect(makeShapeInfoVariable(std::vector<int>{2,3,4,5}, SHAPE_MSG_INPUT0), makeShapeInfoVariable(std::vector<int>{2,3,4,7}, SHAPE_MSG_INPUT1),
                     [](const std::vector<int>& l, const std::vector<int>& r){
                         if(l.size()!=r.size()) return false;
                         for (int i = 0; i < l.size(); i++) {
                             if (l.at(i) != r.at(i)) {
                                 return false;
                             }
                         }
                         return true;
                     }
                  , EXPECTED_EQ_MSG);
    }

Output:

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]
  1. throw error when there is failure

    Requirements req8("Requirement Helper Example#8");
    req8.expectEq(6,6) &&
    req8.expectIn(6, {1,2,3,7,8,9});
    req8.throws();

    Output:

    terminate called after throwing an instance of 'std::invalid_argument'
    what():  Op validation failed
    ...
    Requirement Helper Example#8: {6} expected to be one of these {[1, 2, 3, 7, 8, 9, ]}

Here is live example:

PreviousOverview of working with libnd4jNextExplanation

Last updated 3 years ago

Was this helpful?

Note: some classes were mocked there and do not represent the exact implementations in libnd4j.

https://godbolt.org/z/sq98vchs5