SlideShare a Scribd company logo
1 of 37
Download to read offline
Commons Nabla

    Phil Steitz
Apachecon US, 2011
Agenda
• Motivation - why differentiate bytecode?
• Current API
• How Nabla works internally
• Getting involved - welcome to Commons!




                   2
Motivation

Who needs derivatives?

• Lots of numerical algorithms use derivatives or gradients directly
• Derivatives can be useful in diagnostics:
   – Bounding errors
   – Estimating the impacts of parameter or model changes
• Forecast or describe the local behavior of functions




                              3
Motivation
But painful and error-prone to compute...
• Two choices:
  – Code analytical derivatives (differentiate functions and code
    derivative functions manually)
  – Use numerical differentiation techniques
• Manual coding takes time, is error prone, and can be difficult
  when functions have complex definitions involving loops and
  conditionals
• Numerical differentiation is not exact, requires configuration
  settings that can be tricky to determine for complex functions,
  and has to be done piecewise for functions with conditional
  branching




                               4
Motivation
What if it were possible to get exact derivatives generated
automatically by analyzing and engineering the byte code of
source functions?

‣ No need to manually code derivatives
‣ No need to select algorithms or choose configuration
   parameters for numerical differentiation
‣ No need to deal with piecewise definitions, singularities
   separately
‣ No separate compilation or code generation




                         5
Nabla API
Classes including differentiable functions implement
UnivariateDifferentiable interface
UnivariateDifferentiator implementations provided by Nabla
differentiate bytecode
UnivariateDifferentiable classes get inner classes attached,
including derivative functions
The derivative of a function double f(double t) is a new
function DifferentialPair f(DifferentialPair t) in the
derivative class




                          6
Key Abstractions
public class DifferentialPair implements Serializable {
 ...
     /** Value part of the differential pair. */
     private final double value;

    /** First derivative part of the differential pair. */
    private final double firstDerivative;

    public DifferentialPair(final double u0, final double u1) {
        this.value = u0;
        this.firstDerivative = u1;
    }


Represents a (value, derivative) pair




                              7
Key Abstractions
public interface UnivariateDerivative {

     /**
      * The original function associated with this differential.
      */
     UnivariateDifferentiable getPrimitive();

     /**
      * The derivative of the original function.
      */
     DifferentialPair f(DifferentialPair t);

}


    Differentiators produce derivatives bound to the
    original function via the primitive
    Derivatives take DifferentialPairs as arguments



                                    8
Example
UnivariateDifferentiable function = new UnivariateDifferentiable() {
    public double f(double t) {
        return t * t + 2 * t + 2;
     }
 };

UnivariateDifferentiator differentiator =
    new ForwardModeAlgorithmicDifferentiator();
UnivariateDerivative derivative =
    differentiator.differentiate(function);

DifferentialPair t = DifferentialPair.newVariable(1);

derivative.f(t).getFirstDerivative(); // 2*1 + 2 = 4

derivative.f(t).getValue(); // returns f(1) = 5

derivative.f(new DifferentialPair(1, 2)).getFirstDerivative();
// returns "chain rule" result: 2 * 4 = 8




                             9
Instance Data
public class ParametricFunction implements UnivariateDifferentiable {
    ...
    public void setX(double x) {
        this.x = x;
    }
    public double f(double t) {
        return t * t + x * t + 1;
    }



ParametricFunction function = new ParametricFunction(1);
final UnivariateDerivative derivative =
    new ForwardModeAlgorithmicDifferentiator().differentiate(function);

DifferentialPair t = DifferentialPair.newVariable(1);
derivative.f(t).getFirstDerivative(); // returns 2t + 1 = 3

function.setX(2);
derivative.f(t).getFirstDerivative(); // now returns 4




                                  10
Singularity
UnivariateDifferentiable kinkFunction = new UnivariateDifferentiable() {
    public double f(double t) {
        if (t < 10) {
            return t;
        } else {
            return 2 * t;
        }
    }
};
UnivariateDifferentiator differentiator =
    new ForwardModeAlgorithmicDifferentiator();
UnivariateDerivative derivative = differentiator.differentiate(kinkFunction);

derivative.f(DifferentialPair.newVariable(5)).
       getFirstDerivative(); // Returns 1

derivative.f(DifferentialPair.newVariable(10)).
       getFirstDerivative(); // Returns 2




                                 11
Partial Derivatives
public class SetableParameterModel implements UnivariateDifferentiable {

        private Model model;
        private double firstParameter;

        public SetableParameterModel(Model model, double firstParameter) {
            this.model = model;
            this.firstParameter = firstParameter;
        }

        public void setFirstParameter(double firstParameter) {
            this.firstParameter = firstParameter;
        }

        public double f(double t) {
            return model.evaluate(firstParameter, t);
        }

    }

        Derivative will be partial derivative of model with respect to the second
        variable (for fixed first variable)


                                    12
DifferentialPair Operations

/**
  * Multiplication operator for differential pairs.
  *
  * @param a left hand side parameter of the operator
  * @param b right hand side parameter of the operator
  * @return a&times;b
  */
public static DifferentialPair multiply(final DifferentialPair a,
                                         final DifferentialPair b) {
     return new DifferentialPair(a.value * b.value,
            a.firstDerivative * b.value + a.value * b.firstDerivative);
}




                               13
Implementation Strategy
Create an inner class containing
the derivative function
public DifferentialPair f(DifferentialPair differentialPair)


and a reference to the parent instance
public UnivariateDifferentiable getPrimitive()


“Surgically attached” derivative instance can
refer to instance data of the parent class
‣ State changes can be reflected in derivatives




                           14
15
Implementation Strategy
public UnivariateDerivative differentiate(final UnivariateDifferentiable d)
     throws DifferentiationException {

     // get the derivative class
1    final Class<? extends UnivariateDerivative> derivativeClass =
         getDerivativeClass(d.getClass());

     // get the constructor for the child class
2    final Constructor<? extends UnivariateDerivative> constructor =
         derivativeClass.getConstructor(d.getClass());

3    return constructor.newInstance(d);


    Transform bytecode to create derivative class
    Include a constructor that takes an instance of the parent
    Create an instance attached to the class being differentiated




                                   16
Algorithmic Bytecode Differentiation
Analyze the bytecode of f(t), tracing the data flow from t to the final result
and differentiate the bytecode of the instructions that consume or produce
values along the way.
Look at the instructions that transform the value of t as a composite function:

t ! f0 (t) ! f1 (f0 (t), t) ! . . . ! fn (fn            1 (. . . , t), . . . , t)


If we carry along the value of the derivative at t at each stage, we can
differentiate the composite sequentially:

(t, dt) !
           0
 f0 (t) , f0 (t)dt !
                                  0
 f1 (f0 (t), t) , f11 (f0 (t), t)f0 (t)dt + f12 (t)dt ! ...


                                  17
Example

    2     2           2
t ! t ! t + 2t ! t + 2t + 2


           2
(t, dt) ! t , 2tdt !
  2
 t + 2t , 2tdt + 2dt !
  2
 t + 2t + 2 , 2tdt + 2dt




               18
Transformation Strategy
      Identify instructions that use or produce values descended from the
      actual parameter and transform them to work with DifferentialPairs


      Stack      Values descended                  Values descended
                      from t                          from (t, dt)
                                         1:n
Instructions                                          Instruction(s)
                     Instruction
                                                     augmented to
                                                       operate on
                                                   extended value set


      Key insight: this can be done locally - one instruction at a time
      Function bytecode can be differentiated as a stream




                                    19
Example
   Multiplication - DMUL instruction operating on t, t (to compute t2)

    ‣ Assume derivative computation starts with t dt t dt on the stack
    ‣ Replace single DMUL instruction operating on t t stack with a
      sequence that works on t dt t dt to yield derivative pair
                      Initial Stack           Final Stack

Original
                           tt                       t2
(DMUL)

Derivative              t dt t dt                 t2 2tdt
Instructions

      Does not matter what values start on the stack - just arrange to
      have v dv in place of v when derivative sequence starts
      Code sequences like this for all basic instructions and built-ins
      from Java.Math and “derivative streaming” just works!


                                      20
Example
                                       Bytecode       Stack
                                      public f(D)D
                                          DLOAD 1       t
Function to differentiate                 DLOAD 1       t t
                                          DMUL          t2
public double f(double t) {               LDC 2.0       t2 2
    return t * t + 2 * t + 2;             DLOAD 1       t2 2 t
}                                         DMUL          t2 2t
                                          DADD          t2+2t
                                          LDC 2.0       t2+2t 2
                                          DADD          t2+2t+2
                                          DRETURN
                                          MAXSTACK = 6
                                          MAXLOCALS = 3
                                     }




                                21
Example
public f(Lorg/.../DifferentialPair;) Lorg/.../DifferentialPair;
    ALOAD 1
    DUP
    INVOKEVIRTUAL org/.../DifferentialPair.getValue ()D
    DSTORE 1
    INVOKEVIRTUAL org/.../DifferentialPair.getFirstDerivative ()D
    DSTORE 3
    DLOAD 1
    DLOAD 3
    DLOAD 1
    DLOAD 3


  Initial Stack: t dt t dt




                                 22
Example
                                 t dt t dt
 public f(D)D         DSTORE 9   t dt t        dt -> 9
     DLOAD 1          DUP2       t dt t t
     DLOAD 1          DSTORE 7   t dt t        t -> 7
     DMUL             DMUL       t dt*t
     LDC 2.0          DSTORE 5   t             dt*t -> 5
     DLOAD 1          DUP2       t t
     DMUL             DLOAD 9    t t dt
     DADD             DMUL       t t*dt
     LDC 2.0          DLOAD 5    t t*dt dt*t
     DADD             DADD       t 2tdt
     DRETURN          DSTORE 9   t             2tdt -> 9
     MAXSTACK = 6     DLOAD 7    t t
     MAXLOCALS = 3    DMUL       t2
}                     DLOAD 9    t2 2tdt




                         23
Example

    public f(D)D                    t2   2tdt
       DLOAD 1           LDC 2.0    t2   2tdt   2
       DLOAD 1           DLOAD 1    t2   2tdt   2 t
       DMUL              DLOAD 3    t2   2tdt   2 t dt
       LDC 2.0           DSTORE 9   t2   2tdt   2 t      dt->9
       DLOAD 1           DUP2_X2    t2   2tdt   t 2 t
       DMUL              POP2       t2   2tdt   t 2
       DADD              DUP2       t2   2tdt   t 2 2
       LDC 2.0           DLOAD 9    t2   2tdt   t 2 2 dt
       DADD              DMUL       t2   2tdt   t 2 2dt
       DRETURN           DSTORE 9   t2   2tdt   t 2      2dt->9
       MAXSTACK = 6      DMUL       t2   2tdt   2t
       MAXLOCALS = 3     DLOAD 9    t2   2tdt   2t 2dt
}




                         24
Example

 public f(D)D
     DLOAD 1
                                 t2 2tdt 2t 2dt
     DLOAD 1
                     DSTORE 9    t2 2tdt 2t       2dt -> 9
     DMUL
                     DUP2_X2     t2 2t 2tdt 2t
     LDC 2.0
                     POP2        t2 2t 2tdt
     DLOAD 1
                     DLOAD 9     t2 2t 2tdt 2dt
     DMUL
                     DADD        t2 2t 2tdt+2dt
     DADD
                     DSTORE 9    t2, 2t           2tdt+2dt -> 9
     LDC 2.0
                     DADD        t2 + 2t
     DADD
                     DLOAD 9     t2 + 2t, 2tdt + 2dt
     DRETURN
     MAXSTACK = 6
     MAXLOCALS = 3
}




                            25
Example

public f(D)D
    DLOAD 1
    DLOAD 1
    DMUL                            t2+2t 2tdt+2dt
    LDC 2.0         LDC 2.0         t2+2t 2tdt+2dt 2
    DLOAD 1         DUP2_X2         t2+2t 2 2tdt+2dt 2
    DMUL            POP2            t2+2t 2 2tdt+2dt
    DADD            DSTORE 9        t2+2t 2          2tdt+2dt -> 9
    LDC 2.0         DADD            t2+2t+2
    DADD            DLOAD 9         t2+2t+2 2tdt+2dt
    DRETURN
    MAXSTACK = 6
    MAXLOCALS = 3
}




                               26
Example

 public f(D)D
     DLOAD 1
                                t2+2t+2 2tdt+2dt
     DLOAD 1
                     DSTORE 3   t2+2t+2          2tdt+2dt -> 3
     DMUL
                     DSTORE 1                    t2+2t+2 -> 1
     LDC 2.0
                     NEW org/.../DifferentialPair
     DLOAD 1
                     DUP
     DMUL
                     DLOAD 1    t2+2t+2
     DADD
                     DLOAD 3    t2+2t+2 2tdt+2dt
     LDC 2.0
                     INVOKESPECIAL org/.../DifferentialPair.<init> (DD)V
     DADD
                     ARETURN
     DRETURN
     MAXSTACK = 6
     MAXLOCALS = 3
}




                             27
Some Details
Three problems to solve:

 1. Creating the derivative class and instantiating it
 2. Data flow analysis
 3. Differentiating instructions




                       28
Instantiates derivative class


            creates derived class




29
Implementation Strategy
public UnivariateDerivative differentiate(final UnivariateDifferentiable d)
     throws DifferentiationException {

     // get the derivative class
1    final Class<? extends UnivariateDerivative> derivativeClass =
         getDerivativeClass(d.getClass());

     // load the constructor for the child
2    final Constructor<? extends UnivariateDerivative> constructor =
         derivativeClass.getConstructor(d.getClass());

3    return constructor.newInstance(d);


    Transform bytecode to create derivative class
    Include a constructor that takes an instance of the parent
    Create an instance attached to the class being differentiated




                                   30
differentiateMethod in MethodDifferentiator
public void differentiate(final String primitiveName, final MethodNode method)
       throws DifferentiationException {
       try {
       ...
       // add spare cells to hold new variables if needed
       addSpareLocalVariables(method.instructions);

       // analyze the original code, tracing values production/consumption
       final FlowAnalyzer analyzer =
           new FlowAnalyzer(new TrackingInterpreter(), method.instructions);
       final Frame[] array = analyzer.analyze(primitiveName, method);
       ...
       // identify the needed changes
       final Set<AbstractInsnNode> changes = identifyChanges(method.instructions);
       ...
       // perform the code changes
       changeCode(method.instructions, changes);
       ...
       // change the method properties to the derivative ones
       method.desc       = Descriptors.DP_RETURN_DP_DESCRIPTOR;
       method.access    |= Opcodes.ACC_SYNTHETIC;
       method.maxLocals = maxVariables();




                                      31
differentiateMethod in MethodDifferentiator

ASM analyzer does semantic analysis of instructions, revealing symbolic
state of stack frame at each instruction execution step
TrackingInterpreter keeps track of which values are produced and consumed
by which instructions

    // analyze the original code, tracing values production/consumption
    final FlowAnalyzer analyzer =
        new FlowAnalyzer(new TrackingInterpreter(), method.instructions);
    final Frame[] array = analyzer.analyze(primitiveName, method);




                                    32
differentiateMethod in MethodDifferentiator


  // identify the needed changes
  final Set<AbstractInsnNode> changes = identifyChanges(method.instructions);
  ...



Identification of instructions needing change is based on data flow analysis
1. Change the local variables in the initial frame to match the parameters of
   the derivative method
2. Propagate these variables following the instructions path, updating stack
   cells and local variables as needed
     ➡ Instructions that must be changed are the ones that consume
        changed variables or stack cells




                                    33
changeCode in MethodDifferentiator
private void changeCode(final InsnList instructions, final Set<AbstractInsnNode> changes)
        throws DifferentiationException {

    // insert the parameter conversion code at method start
    final InsnList list = new InsnList();
    list.add(new VarInsnNode(Opcodes.ALOAD, 1));
    list.add(new InsnNode(Opcodes.DUP));
    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Descriptors.DP_NAME,
                                "getValue", Descriptors.VOID_RETURN_D_DESCRIPTOR));
    list.add(new VarInsnNode(Opcodes.DSTORE, 1));
    list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Descriptors.DP_NAME,
             "getFirstDerivative", Descriptors.VOID_RETURN_D_DESCRIPTOR));
    list.add(new VarInsnNode(Opcodes.DSTORE, 3));

    instructions.insertBefore(instructions.get(0), list);

    // transform the existing instructions
    for (final AbstractInsnNode insn : changes) {
        instructions.insert(insn, getReplacement(insn));
        instructions.remove(insn);
    }

   1. Get “t dt” on the stack from the input DifferentialPair
   2. Selectively transform instructions needing change “in place”




                                      34
Example instruction replacement - for DMUL
instructions with both arguments DifferentialPairs

public InsnList getReplacement(final AbstractInsnNode insn,
                                   final MethodDifferentiator methodDifferentiator)
    throws DifferentiationException {
    // Create tmp1-tmp3 temp vars, list as instruction list
    ...
    // operand stack initial state: a0, a1, b0, b1
    list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0, a1, b0
    list.add(new InsnNode(Opcodes.DUP2));            // => a0, a1, b0, b0
    list.add(new VarInsnNode(Opcodes.DSTORE, tmp2)); // => a0, a1, b0
    list.add(new InsnNode(Opcodes.DMUL));            // => a0, a1*b0
    list.add(new VarInsnNode(Opcodes.DSTORE, tmp3)); // => a0
    list.add(new InsnNode(Opcodes.DUP2));            // => a0, a0
    list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0, a0, b1
    list.add(new InsnNode(Opcodes.DMUL));            // => a0, a0*b1
    list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => a0, a0*b1, a1*b0
    list.add(new InsnNode(Opcodes.DADD));            // => a0, a0*b1+a1*b0
    list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0
    list.add(new VarInsnNode(Opcodes.DLOAD, tmp2)); // => a0, b0
    list.add(new InsnNode(Opcodes.DMUL));            // => a0*b0
    list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0*b0, a0*b1+a1*b0

   return list;




                                     35
Work in Progress
• GETFIELD does not really work yet
  – Despite being an inner class, we don’t have
    access out of the box (hidden accessors not
    created for us)
• INVOKEVIRTUAL does not work at all yet
  – Vision is to maintain a stack and differentiate
    all relevant methods of any class involved
• Direct support for partial derivatives and
  gradients
• Reverse mode differentiation

                      36
Get Involved!
1. Subscribe to Commons-dev
    http://commons.apache.org/mail-lists.html
2. Check out the code
    http://commons.apache.org/svninfo.html
3. Review open issues
    http://commons.apache.org/sandbox/issue-tracking.html
4. Talk about your ideas
5. Attach patches to JIRA tickets
   http://commons.apache.org/patches.html
6. THANKS!!

                          37

More Related Content

What's hot

Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Life & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentLife & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentPersistent Systems Ltd.
 
C programming session 07
C programming session 07C programming session 07
C programming session 07AjayBahoriya
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4Richard Jones
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5Richard Jones
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppteShikshak
 
Materi 6 user definedfunction
Materi 6 user definedfunctionMateri 6 user definedfunction
Materi 6 user definedfunctionAl Frilantika
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, RecursionSreedhar Chowdam
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 

What's hot (20)

Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Functions
FunctionsFunctions
Functions
 
C++ Chapter IV
C++ Chapter IVC++ Chapter IV
C++ Chapter IV
 
Life & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@PersistentLife & Work of Robin Milner | Turing100@Persistent
Life & Work of Robin Milner | Turing100@Persistent
 
C programming session 07
C programming session 07C programming session 07
C programming session 07
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
What's New In Python 2.4
What's New In Python 2.4What's New In Python 2.4
What's New In Python 2.4
 
What's New In Python 2.5
What's New In Python 2.5What's New In Python 2.5
What's New In Python 2.5
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Chtp414
Chtp414Chtp414
Chtp414
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Materi 6 user definedfunction
Materi 6 user definedfunctionMateri 6 user definedfunction
Materi 6 user definedfunction
 
Advance python
Advance pythonAdvance python
Advance python
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
C++ Chapter I
C++ Chapter IC++ Chapter I
C++ Chapter I
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
First fare 2010 java-introduction
First fare 2010 java-introductionFirst fare 2010 java-introduction
First fare 2010 java-introduction
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 

Similar to Commons Nabla

The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf4babies2010
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfezonesolutions
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Hermann Hueck
 
Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2IIUM
 
User defined functions
User defined functionsUser defined functions
User defined functionsshubham_jangid
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... FunctionsMichal Bigos
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)Ishin Vin
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядомAndrey Akinshin
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Lab manual uoh_ee370
Lab manual uoh_ee370Lab manual uoh_ee370
Lab manual uoh_ee370slatano
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfmanjan6
 
C language presentation
C language presentationC language presentation
C language presentationbainspreet
 

Similar to Commons Nabla (20)

The following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdfThe following is the (incomplete) header file for the class Fracti.pdf
The following is the (incomplete) header file for the class Fracti.pdf
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
 
Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)Composing an App with Free Monads (using Cats)
Composing an App with Free Monads (using Cats)
 
Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2
 
Functions12
Functions12Functions12
Functions12
 
Functions123
Functions123 Functions123
Functions123
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
All About ... Functions
All About ... FunctionsAll About ... Functions
All About ... Functions
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
.NET 2015: Будущее рядом
.NET 2015: Будущее рядом.NET 2015: Будущее рядом
.NET 2015: Будущее рядом
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Lab manual uoh_ee370
Lab manual uoh_ee370Lab manual uoh_ee370
Lab manual uoh_ee370
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Help in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdfHelp in JAVAThis program should input numerator and denominator f.pdf
Help in JAVAThis program should input numerator and denominator f.pdf
 
C language presentation
C language presentationC language presentation
C language presentation
 
Unit 8
Unit 8Unit 8
Unit 8
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 

More from Phil Steitz

Programming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathProgramming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathPhil Steitz
 
Apache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdateApache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdatePhil Steitz
 
Leadership model
Leadership modelLeadership model
Leadership modelPhil Steitz
 
Open Development in the Enterprise
Open Development in the EnterpriseOpen Development in the Enterprise
Open Development in the EnterprisePhil Steitz
 
Commons Pool and DBCP
Commons Pool and DBCPCommons Pool and DBCP
Commons Pool and DBCPPhil Steitz
 
Principles of Technology Leadership
Principles of Technology LeadershipPrinciples of Technology Leadership
Principles of Technology LeadershipPhil Steitz
 

More from Phil Steitz (7)

Programming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons MathProgramming Math in Java - Lessons from Apache Commons Math
Programming Math in Java - Lessons from Apache Commons Math
 
Apache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 UpdateApache Commons Pool and DBCP - Version 2 Update
Apache Commons Pool and DBCP - Version 2 Update
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Leadership model
Leadership modelLeadership model
Leadership model
 
Open Development in the Enterprise
Open Development in the EnterpriseOpen Development in the Enterprise
Open Development in the Enterprise
 
Commons Pool and DBCP
Commons Pool and DBCPCommons Pool and DBCP
Commons Pool and DBCP
 
Principles of Technology Leadership
Principles of Technology LeadershipPrinciples of Technology Leadership
Principles of Technology Leadership
 

Recently uploaded

The next social challenge to public health: the information environment.pptx
The next social challenge to public health:  the information environment.pptxThe next social challenge to public health:  the information environment.pptx
The next social challenge to public health: the information environment.pptxTina Purnat
 
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdf
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdfMedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdf
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdfSasikiranMarri
 
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptx
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptxL1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptx
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptxDr Bilal Natiq
 
Biomechanics- Shoulder Joint!!!!!!!!!!!!
Biomechanics- Shoulder Joint!!!!!!!!!!!!Biomechanics- Shoulder Joint!!!!!!!!!!!!
Biomechanics- Shoulder Joint!!!!!!!!!!!!ibtesaam huma
 
medico legal aspects of wound - forensic medicine
medico legal aspects of wound - forensic medicinemedico legal aspects of wound - forensic medicine
medico legal aspects of wound - forensic medicinethanaram patel
 
Plant Fibres used as Surgical Dressings PDF.pdf
Plant Fibres used as Surgical Dressings PDF.pdfPlant Fibres used as Surgical Dressings PDF.pdf
Plant Fibres used as Surgical Dressings PDF.pdfDivya Kanojiya
 
Measurement of Radiation and Dosimetric Procedure.pptx
Measurement of Radiation and Dosimetric Procedure.pptxMeasurement of Radiation and Dosimetric Procedure.pptx
Measurement of Radiation and Dosimetric Procedure.pptxDr. Dheeraj Kumar
 
LUNG TUMORS AND ITS CLASSIFICATIONS.pdf
LUNG TUMORS AND ITS  CLASSIFICATIONS.pdfLUNG TUMORS AND ITS  CLASSIFICATIONS.pdf
LUNG TUMORS AND ITS CLASSIFICATIONS.pdfDolisha Warbi
 
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptx
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptxPresentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptx
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptxpdamico1
 
PHYSIOTHERAPY IN HEART TRANSPLANTATION..
PHYSIOTHERAPY IN HEART TRANSPLANTATION..PHYSIOTHERAPY IN HEART TRANSPLANTATION..
PHYSIOTHERAPY IN HEART TRANSPLANTATION..AneriPatwari
 
Clinical Pharmacotherapy of Scabies Disease
Clinical Pharmacotherapy of Scabies DiseaseClinical Pharmacotherapy of Scabies Disease
Clinical Pharmacotherapy of Scabies DiseaseSreenivasa Reddy Thalla
 
Big Data Analysis Suggests COVID Vaccination Increases Excess Mortality Of ...
Big Data Analysis Suggests COVID  Vaccination Increases Excess Mortality Of  ...Big Data Analysis Suggests COVID  Vaccination Increases Excess Mortality Of  ...
Big Data Analysis Suggests COVID Vaccination Increases Excess Mortality Of ...sdateam0
 
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMA
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMAANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMA
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMADivya Kanojiya
 
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptx
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptxPERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptx
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptxdrashraf369
 
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdf
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdfPULMONARY EMBOLISM AND ITS MANAGEMENTS.pdf
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdfDolisha Warbi
 
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic Analysis
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic AnalysisVarSeq 2.6.0: Advancing Pharmacogenomics and Genomic Analysis
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic AnalysisGolden Helix
 
epilepsy and status epilepticus for undergraduate.pptx
epilepsy and status epilepticus  for undergraduate.pptxepilepsy and status epilepticus  for undergraduate.pptx
epilepsy and status epilepticus for undergraduate.pptxMohamed Rizk Khodair
 
Culture and Health Disorders Social change.pptx
Culture and Health Disorders Social change.pptxCulture and Health Disorders Social change.pptx
Culture and Health Disorders Social change.pptxDr. Dheeraj Kumar
 
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand University
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand UniversityCEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand University
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand UniversityHarshChauhan475104
 
COVID-19 (NOVEL CORONA VIRUS DISEASE PANDEMIC ).pptx
COVID-19  (NOVEL CORONA  VIRUS DISEASE PANDEMIC ).pptxCOVID-19  (NOVEL CORONA  VIRUS DISEASE PANDEMIC ).pptx
COVID-19 (NOVEL CORONA VIRUS DISEASE PANDEMIC ).pptxBibekananda shah
 

Recently uploaded (20)

The next social challenge to public health: the information environment.pptx
The next social challenge to public health:  the information environment.pptxThe next social challenge to public health:  the information environment.pptx
The next social challenge to public health: the information environment.pptx
 
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdf
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdfMedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdf
MedDRA-A-Comprehensive-Guide-to-Standardized-Medical-Terminology.pdf
 
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptx
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptxL1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptx
L1.INTRODUCTION to ENDOCRINOLOGY MEDICINE.pptx
 
Biomechanics- Shoulder Joint!!!!!!!!!!!!
Biomechanics- Shoulder Joint!!!!!!!!!!!!Biomechanics- Shoulder Joint!!!!!!!!!!!!
Biomechanics- Shoulder Joint!!!!!!!!!!!!
 
medico legal aspects of wound - forensic medicine
medico legal aspects of wound - forensic medicinemedico legal aspects of wound - forensic medicine
medico legal aspects of wound - forensic medicine
 
Plant Fibres used as Surgical Dressings PDF.pdf
Plant Fibres used as Surgical Dressings PDF.pdfPlant Fibres used as Surgical Dressings PDF.pdf
Plant Fibres used as Surgical Dressings PDF.pdf
 
Measurement of Radiation and Dosimetric Procedure.pptx
Measurement of Radiation and Dosimetric Procedure.pptxMeasurement of Radiation and Dosimetric Procedure.pptx
Measurement of Radiation and Dosimetric Procedure.pptx
 
LUNG TUMORS AND ITS CLASSIFICATIONS.pdf
LUNG TUMORS AND ITS  CLASSIFICATIONS.pdfLUNG TUMORS AND ITS  CLASSIFICATIONS.pdf
LUNG TUMORS AND ITS CLASSIFICATIONS.pdf
 
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptx
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptxPresentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptx
Presentation for Bella Mahl 2024-03-28-24-MW-Overview-Bella.pptx
 
PHYSIOTHERAPY IN HEART TRANSPLANTATION..
PHYSIOTHERAPY IN HEART TRANSPLANTATION..PHYSIOTHERAPY IN HEART TRANSPLANTATION..
PHYSIOTHERAPY IN HEART TRANSPLANTATION..
 
Clinical Pharmacotherapy of Scabies Disease
Clinical Pharmacotherapy of Scabies DiseaseClinical Pharmacotherapy of Scabies Disease
Clinical Pharmacotherapy of Scabies Disease
 
Big Data Analysis Suggests COVID Vaccination Increases Excess Mortality Of ...
Big Data Analysis Suggests COVID  Vaccination Increases Excess Mortality Of  ...Big Data Analysis Suggests COVID  Vaccination Increases Excess Mortality Of  ...
Big Data Analysis Suggests COVID Vaccination Increases Excess Mortality Of ...
 
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMA
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMAANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMA
ANTI-DIABETICS DRUGS - PTEROCARPUS AND GYMNEMA
 
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptx
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptxPERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptx
PERFECT BUT PAINFUL TKR -ROLE OF SYNOVECTOMY.pptx
 
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdf
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdfPULMONARY EMBOLISM AND ITS MANAGEMENTS.pdf
PULMONARY EMBOLISM AND ITS MANAGEMENTS.pdf
 
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic Analysis
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic AnalysisVarSeq 2.6.0: Advancing Pharmacogenomics and Genomic Analysis
VarSeq 2.6.0: Advancing Pharmacogenomics and Genomic Analysis
 
epilepsy and status epilepticus for undergraduate.pptx
epilepsy and status epilepticus  for undergraduate.pptxepilepsy and status epilepticus  for undergraduate.pptx
epilepsy and status epilepticus for undergraduate.pptx
 
Culture and Health Disorders Social change.pptx
Culture and Health Disorders Social change.pptxCulture and Health Disorders Social change.pptx
Culture and Health Disorders Social change.pptx
 
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand University
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand UniversityCEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand University
CEHPALOSPORINS.pptx By Harshvardhan Dev Bhoomi Uttarakhand University
 
COVID-19 (NOVEL CORONA VIRUS DISEASE PANDEMIC ).pptx
COVID-19  (NOVEL CORONA  VIRUS DISEASE PANDEMIC ).pptxCOVID-19  (NOVEL CORONA  VIRUS DISEASE PANDEMIC ).pptx
COVID-19 (NOVEL CORONA VIRUS DISEASE PANDEMIC ).pptx
 

Commons Nabla

  • 1. Commons Nabla Phil Steitz Apachecon US, 2011
  • 2. Agenda • Motivation - why differentiate bytecode? • Current API • How Nabla works internally • Getting involved - welcome to Commons! 2
  • 3. Motivation Who needs derivatives? • Lots of numerical algorithms use derivatives or gradients directly • Derivatives can be useful in diagnostics: – Bounding errors – Estimating the impacts of parameter or model changes • Forecast or describe the local behavior of functions 3
  • 4. Motivation But painful and error-prone to compute... • Two choices: – Code analytical derivatives (differentiate functions and code derivative functions manually) – Use numerical differentiation techniques • Manual coding takes time, is error prone, and can be difficult when functions have complex definitions involving loops and conditionals • Numerical differentiation is not exact, requires configuration settings that can be tricky to determine for complex functions, and has to be done piecewise for functions with conditional branching 4
  • 5. Motivation What if it were possible to get exact derivatives generated automatically by analyzing and engineering the byte code of source functions? ‣ No need to manually code derivatives ‣ No need to select algorithms or choose configuration parameters for numerical differentiation ‣ No need to deal with piecewise definitions, singularities separately ‣ No separate compilation or code generation 5
  • 6. Nabla API Classes including differentiable functions implement UnivariateDifferentiable interface UnivariateDifferentiator implementations provided by Nabla differentiate bytecode UnivariateDifferentiable classes get inner classes attached, including derivative functions The derivative of a function double f(double t) is a new function DifferentialPair f(DifferentialPair t) in the derivative class 6
  • 7. Key Abstractions public class DifferentialPair implements Serializable { ... /** Value part of the differential pair. */ private final double value; /** First derivative part of the differential pair. */ private final double firstDerivative; public DifferentialPair(final double u0, final double u1) { this.value = u0; this.firstDerivative = u1; } Represents a (value, derivative) pair 7
  • 8. Key Abstractions public interface UnivariateDerivative { /** * The original function associated with this differential. */ UnivariateDifferentiable getPrimitive(); /** * The derivative of the original function. */ DifferentialPair f(DifferentialPair t); } Differentiators produce derivatives bound to the original function via the primitive Derivatives take DifferentialPairs as arguments 8
  • 9. Example UnivariateDifferentiable function = new UnivariateDifferentiable() { public double f(double t) { return t * t + 2 * t + 2; } }; UnivariateDifferentiator differentiator = new ForwardModeAlgorithmicDifferentiator(); UnivariateDerivative derivative = differentiator.differentiate(function); DifferentialPair t = DifferentialPair.newVariable(1); derivative.f(t).getFirstDerivative(); // 2*1 + 2 = 4 derivative.f(t).getValue(); // returns f(1) = 5 derivative.f(new DifferentialPair(1, 2)).getFirstDerivative(); // returns "chain rule" result: 2 * 4 = 8 9
  • 10. Instance Data public class ParametricFunction implements UnivariateDifferentiable { ... public void setX(double x) { this.x = x; } public double f(double t) { return t * t + x * t + 1; } ParametricFunction function = new ParametricFunction(1); final UnivariateDerivative derivative = new ForwardModeAlgorithmicDifferentiator().differentiate(function); DifferentialPair t = DifferentialPair.newVariable(1); derivative.f(t).getFirstDerivative(); // returns 2t + 1 = 3 function.setX(2); derivative.f(t).getFirstDerivative(); // now returns 4 10
  • 11. Singularity UnivariateDifferentiable kinkFunction = new UnivariateDifferentiable() { public double f(double t) { if (t < 10) { return t; } else { return 2 * t; } } }; UnivariateDifferentiator differentiator = new ForwardModeAlgorithmicDifferentiator(); UnivariateDerivative derivative = differentiator.differentiate(kinkFunction); derivative.f(DifferentialPair.newVariable(5)). getFirstDerivative(); // Returns 1 derivative.f(DifferentialPair.newVariable(10)). getFirstDerivative(); // Returns 2 11
  • 12. Partial Derivatives public class SetableParameterModel implements UnivariateDifferentiable { private Model model; private double firstParameter; public SetableParameterModel(Model model, double firstParameter) { this.model = model; this.firstParameter = firstParameter; } public void setFirstParameter(double firstParameter) { this.firstParameter = firstParameter; } public double f(double t) { return model.evaluate(firstParameter, t); } } Derivative will be partial derivative of model with respect to the second variable (for fixed first variable) 12
  • 13. DifferentialPair Operations /** * Multiplication operator for differential pairs. * * @param a left hand side parameter of the operator * @param b right hand side parameter of the operator * @return a&times;b */ public static DifferentialPair multiply(final DifferentialPair a, final DifferentialPair b) { return new DifferentialPair(a.value * b.value, a.firstDerivative * b.value + a.value * b.firstDerivative); } 13
  • 14. Implementation Strategy Create an inner class containing the derivative function public DifferentialPair f(DifferentialPair differentialPair) and a reference to the parent instance public UnivariateDifferentiable getPrimitive() “Surgically attached” derivative instance can refer to instance data of the parent class ‣ State changes can be reflected in derivatives 14
  • 15. 15
  • 16. Implementation Strategy public UnivariateDerivative differentiate(final UnivariateDifferentiable d) throws DifferentiationException { // get the derivative class 1 final Class<? extends UnivariateDerivative> derivativeClass = getDerivativeClass(d.getClass()); // get the constructor for the child class 2 final Constructor<? extends UnivariateDerivative> constructor = derivativeClass.getConstructor(d.getClass()); 3 return constructor.newInstance(d); Transform bytecode to create derivative class Include a constructor that takes an instance of the parent Create an instance attached to the class being differentiated 16
  • 17. Algorithmic Bytecode Differentiation Analyze the bytecode of f(t), tracing the data flow from t to the final result and differentiate the bytecode of the instructions that consume or produce values along the way. Look at the instructions that transform the value of t as a composite function: t ! f0 (t) ! f1 (f0 (t), t) ! . . . ! fn (fn 1 (. . . , t), . . . , t) If we carry along the value of the derivative at t at each stage, we can differentiate the composite sequentially: (t, dt) ! 0 f0 (t) , f0 (t)dt ! 0 f1 (f0 (t), t) , f11 (f0 (t), t)f0 (t)dt + f12 (t)dt ! ... 17
  • 18. Example 2 2 2 t ! t ! t + 2t ! t + 2t + 2 2 (t, dt) ! t , 2tdt ! 2 t + 2t , 2tdt + 2dt ! 2 t + 2t + 2 , 2tdt + 2dt 18
  • 19. Transformation Strategy Identify instructions that use or produce values descended from the actual parameter and transform them to work with DifferentialPairs Stack Values descended Values descended from t from (t, dt) 1:n Instructions Instruction(s) Instruction augmented to operate on extended value set Key insight: this can be done locally - one instruction at a time Function bytecode can be differentiated as a stream 19
  • 20. Example Multiplication - DMUL instruction operating on t, t (to compute t2) ‣ Assume derivative computation starts with t dt t dt on the stack ‣ Replace single DMUL instruction operating on t t stack with a sequence that works on t dt t dt to yield derivative pair Initial Stack Final Stack Original tt t2 (DMUL) Derivative t dt t dt t2 2tdt Instructions Does not matter what values start on the stack - just arrange to have v dv in place of v when derivative sequence starts Code sequences like this for all basic instructions and built-ins from Java.Math and “derivative streaming” just works! 20
  • 21. Example Bytecode Stack public f(D)D DLOAD 1 t Function to differentiate DLOAD 1 t t DMUL t2 public double f(double t) { LDC 2.0 t2 2 return t * t + 2 * t + 2; DLOAD 1 t2 2 t } DMUL t2 2t DADD t2+2t LDC 2.0 t2+2t 2 DADD t2+2t+2 DRETURN MAXSTACK = 6 MAXLOCALS = 3 } 21
  • 22. Example public f(Lorg/.../DifferentialPair;) Lorg/.../DifferentialPair; ALOAD 1 DUP INVOKEVIRTUAL org/.../DifferentialPair.getValue ()D DSTORE 1 INVOKEVIRTUAL org/.../DifferentialPair.getFirstDerivative ()D DSTORE 3 DLOAD 1 DLOAD 3 DLOAD 1 DLOAD 3 Initial Stack: t dt t dt 22
  • 23. Example t dt t dt public f(D)D DSTORE 9 t dt t dt -> 9 DLOAD 1 DUP2 t dt t t DLOAD 1 DSTORE 7 t dt t t -> 7 DMUL DMUL t dt*t LDC 2.0 DSTORE 5 t dt*t -> 5 DLOAD 1 DUP2 t t DMUL DLOAD 9 t t dt DADD DMUL t t*dt LDC 2.0 DLOAD 5 t t*dt dt*t DADD DADD t 2tdt DRETURN DSTORE 9 t 2tdt -> 9 MAXSTACK = 6 DLOAD 7 t t MAXLOCALS = 3 DMUL t2 } DLOAD 9 t2 2tdt 23
  • 24. Example public f(D)D t2 2tdt DLOAD 1 LDC 2.0 t2 2tdt 2 DLOAD 1 DLOAD 1 t2 2tdt 2 t DMUL DLOAD 3 t2 2tdt 2 t dt LDC 2.0 DSTORE 9 t2 2tdt 2 t dt->9 DLOAD 1 DUP2_X2 t2 2tdt t 2 t DMUL POP2 t2 2tdt t 2 DADD DUP2 t2 2tdt t 2 2 LDC 2.0 DLOAD 9 t2 2tdt t 2 2 dt DADD DMUL t2 2tdt t 2 2dt DRETURN DSTORE 9 t2 2tdt t 2 2dt->9 MAXSTACK = 6 DMUL t2 2tdt 2t MAXLOCALS = 3 DLOAD 9 t2 2tdt 2t 2dt } 24
  • 25. Example public f(D)D DLOAD 1 t2 2tdt 2t 2dt DLOAD 1 DSTORE 9 t2 2tdt 2t 2dt -> 9 DMUL DUP2_X2 t2 2t 2tdt 2t LDC 2.0 POP2 t2 2t 2tdt DLOAD 1 DLOAD 9 t2 2t 2tdt 2dt DMUL DADD t2 2t 2tdt+2dt DADD DSTORE 9 t2, 2t 2tdt+2dt -> 9 LDC 2.0 DADD t2 + 2t DADD DLOAD 9 t2 + 2t, 2tdt + 2dt DRETURN MAXSTACK = 6 MAXLOCALS = 3 } 25
  • 26. Example public f(D)D DLOAD 1 DLOAD 1 DMUL t2+2t 2tdt+2dt LDC 2.0 LDC 2.0 t2+2t 2tdt+2dt 2 DLOAD 1 DUP2_X2 t2+2t 2 2tdt+2dt 2 DMUL POP2 t2+2t 2 2tdt+2dt DADD DSTORE 9 t2+2t 2 2tdt+2dt -> 9 LDC 2.0 DADD t2+2t+2 DADD DLOAD 9 t2+2t+2 2tdt+2dt DRETURN MAXSTACK = 6 MAXLOCALS = 3 } 26
  • 27. Example public f(D)D DLOAD 1 t2+2t+2 2tdt+2dt DLOAD 1 DSTORE 3 t2+2t+2 2tdt+2dt -> 3 DMUL DSTORE 1 t2+2t+2 -> 1 LDC 2.0 NEW org/.../DifferentialPair DLOAD 1 DUP DMUL DLOAD 1 t2+2t+2 DADD DLOAD 3 t2+2t+2 2tdt+2dt LDC 2.0 INVOKESPECIAL org/.../DifferentialPair.<init> (DD)V DADD ARETURN DRETURN MAXSTACK = 6 MAXLOCALS = 3 } 27
  • 28. Some Details Three problems to solve: 1. Creating the derivative class and instantiating it 2. Data flow analysis 3. Differentiating instructions 28
  • 29. Instantiates derivative class creates derived class 29
  • 30. Implementation Strategy public UnivariateDerivative differentiate(final UnivariateDifferentiable d) throws DifferentiationException { // get the derivative class 1 final Class<? extends UnivariateDerivative> derivativeClass = getDerivativeClass(d.getClass()); // load the constructor for the child 2 final Constructor<? extends UnivariateDerivative> constructor = derivativeClass.getConstructor(d.getClass()); 3 return constructor.newInstance(d); Transform bytecode to create derivative class Include a constructor that takes an instance of the parent Create an instance attached to the class being differentiated 30
  • 31. differentiateMethod in MethodDifferentiator public void differentiate(final String primitiveName, final MethodNode method) throws DifferentiationException { try { ... // add spare cells to hold new variables if needed addSpareLocalVariables(method.instructions); // analyze the original code, tracing values production/consumption final FlowAnalyzer analyzer = new FlowAnalyzer(new TrackingInterpreter(), method.instructions); final Frame[] array = analyzer.analyze(primitiveName, method); ... // identify the needed changes final Set<AbstractInsnNode> changes = identifyChanges(method.instructions); ... // perform the code changes changeCode(method.instructions, changes); ... // change the method properties to the derivative ones method.desc = Descriptors.DP_RETURN_DP_DESCRIPTOR; method.access |= Opcodes.ACC_SYNTHETIC; method.maxLocals = maxVariables(); 31
  • 32. differentiateMethod in MethodDifferentiator ASM analyzer does semantic analysis of instructions, revealing symbolic state of stack frame at each instruction execution step TrackingInterpreter keeps track of which values are produced and consumed by which instructions // analyze the original code, tracing values production/consumption final FlowAnalyzer analyzer = new FlowAnalyzer(new TrackingInterpreter(), method.instructions); final Frame[] array = analyzer.analyze(primitiveName, method); 32
  • 33. differentiateMethod in MethodDifferentiator // identify the needed changes final Set<AbstractInsnNode> changes = identifyChanges(method.instructions); ... Identification of instructions needing change is based on data flow analysis 1. Change the local variables in the initial frame to match the parameters of the derivative method 2. Propagate these variables following the instructions path, updating stack cells and local variables as needed ➡ Instructions that must be changed are the ones that consume changed variables or stack cells 33
  • 34. changeCode in MethodDifferentiator private void changeCode(final InsnList instructions, final Set<AbstractInsnNode> changes) throws DifferentiationException { // insert the parameter conversion code at method start final InsnList list = new InsnList(); list.add(new VarInsnNode(Opcodes.ALOAD, 1)); list.add(new InsnNode(Opcodes.DUP)); list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Descriptors.DP_NAME, "getValue", Descriptors.VOID_RETURN_D_DESCRIPTOR)); list.add(new VarInsnNode(Opcodes.DSTORE, 1)); list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, Descriptors.DP_NAME, "getFirstDerivative", Descriptors.VOID_RETURN_D_DESCRIPTOR)); list.add(new VarInsnNode(Opcodes.DSTORE, 3)); instructions.insertBefore(instructions.get(0), list); // transform the existing instructions for (final AbstractInsnNode insn : changes) { instructions.insert(insn, getReplacement(insn)); instructions.remove(insn); } 1. Get “t dt” on the stack from the input DifferentialPair 2. Selectively transform instructions needing change “in place” 34
  • 35. Example instruction replacement - for DMUL instructions with both arguments DifferentialPairs public InsnList getReplacement(final AbstractInsnNode insn, final MethodDifferentiator methodDifferentiator) throws DifferentiationException { // Create tmp1-tmp3 temp vars, list as instruction list ... // operand stack initial state: a0, a1, b0, b1 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0, a1, b0 list.add(new InsnNode(Opcodes.DUP2)); // => a0, a1, b0, b0 list.add(new VarInsnNode(Opcodes.DSTORE, tmp2)); // => a0, a1, b0 list.add(new InsnNode(Opcodes.DMUL)); // => a0, a1*b0 list.add(new VarInsnNode(Opcodes.DSTORE, tmp3)); // => a0 list.add(new InsnNode(Opcodes.DUP2)); // => a0, a0 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0, a0, b1 list.add(new InsnNode(Opcodes.DMUL)); // => a0, a0*b1 list.add(new VarInsnNode(Opcodes.DLOAD, tmp3)); // => a0, a0*b1, a1*b0 list.add(new InsnNode(Opcodes.DADD)); // => a0, a0*b1+a1*b0 list.add(new VarInsnNode(Opcodes.DSTORE, tmp1)); // => a0 list.add(new VarInsnNode(Opcodes.DLOAD, tmp2)); // => a0, b0 list.add(new InsnNode(Opcodes.DMUL)); // => a0*b0 list.add(new VarInsnNode(Opcodes.DLOAD, tmp1)); // => a0*b0, a0*b1+a1*b0 return list; 35
  • 36. Work in Progress • GETFIELD does not really work yet – Despite being an inner class, we don’t have access out of the box (hidden accessors not created for us) • INVOKEVIRTUAL does not work at all yet – Vision is to maintain a stack and differentiate all relevant methods of any class involved • Direct support for partial derivatives and gradients • Reverse mode differentiation 36
  • 37. Get Involved! 1. Subscribe to Commons-dev http://commons.apache.org/mail-lists.html 2. Check out the code http://commons.apache.org/svninfo.html 3. Review open issues http://commons.apache.org/sandbox/issue-tracking.html 4. Talk about your ideas 5. Attach patches to JIRA tickets http://commons.apache.org/patches.html 6. THANKS!! 37