This document plans only the SequenceProcessing.Functions layer.
It does not implement the functions.
It does not port model classes.
It does not modify ComputationalGraph-C.
Java ComputationalGraph.Function.Function defines three responsibilities:
Tensor calculate(Tensor value)Tensor derivative(Tensor value, Tensor backward)ComputationalNode addEdge(ArrayList<ComputationalNode> inputNodes, boolean isBiased)
Each SequenceProcessing.Functions.* class follows the same graph-construction pattern:
- create a new
FunctionNode(isBiased, this) - connect it to the first input node with
inputNodes.get(0).add(newNode) - return the new node
On the Java side:
- forward evaluation is delegated through the function object stored on the node
- backward evaluation is delegated through
derivative(value, backward)
This means each function class mixes two concerns:
- tensor math
- graph-edge construction
Functionvtable in Function.hcalculate(const void*, const Tensor*)derivative(const void*, const Tensor*, const Tensor*)
Computational_nodein ComputationalNode.h- stores
void* function - stores
valueandbackward
- stores
- graph edge registration in ComputationalGraph.h
add_edge(...)add_addition_edge(...)add_edge_with_hadamard(...)
- derivative dispatch in ComputationalGraph.c
- uses
function->derivative(...)
- uses
- no dedicated
FunctionNodetype - no function-level
addEdge(inputNodes, isBiased)equivalent - no node-local
children/parentsarrays like Java - graph topology is owned by
Computational_graphhash maps, not by nodes
The tensor-math half of the Java functions can be ported locally in SequenceProcessing-C.
The Java addEdge(...) pattern cannot be ported 1:1 without introducing a local compatibility layer, because in C edge creation needs a Computational_graph_ptr, not just an input-node list.
This is an architectural mismatch, but it is not necessarily a blocker in ComputationalGraph-C itself. The existing C graph API is already strong enough to attach function nodes; it just uses a different entry point.
Port the function math locally in SequenceProcessing-C while avoiding any fake FunctionNode clone.
- Each SequenceProcessing function becomes a local C struct whose first field is
Function. - Stateless functions still get their own concrete struct for consistency.
- Stateful functions keep their Java state:
AdditionByConstant.constantMultiplyByConstant.constantSquareRoot.epsilonSwitch.turn
- Each function exposes:
- constructor
- destructor
calculate_*derivative_*
- Graph attachment is handled by a local helper, not by the function structs themselves.
Add a small local compatibility helper later in SequenceProcessing-C, for example:
src/Functions/SequenceFunctionEdge.hsrc/Functions/SequenceFunctionEdge.c
with a shape like:
Computational_node_ptr add_sequence_function_edge(Computational_graph_ptr graph, Computational_node_ptr input, Function* function, bool is_biased);
Implementation would delegate to ComputationalGraph-C:add_edge(...).
This keeps:
- function math local to
SequenceProcessing-C - graph ownership local to
ComputationalGraph-C - model code explicit about which graph it is mutating
- proposed C files:
src/Functions/AdditionByConstant.hsrc/Functions/AdditionByConstant.c
- likely
ComputationalGraph-Cdependency:Function.h- later
ComputationalGraph.honly through local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes, fully for tensor math
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)parity
- notes:
- easy stateful unary transform
- derivative just returns backward unchanged
- proposed C files:
src/Functions/Inverse.hsrc/Functions/Inverse.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- derivative in Java appears mathematically suspicious
- Java source currently uses
-pow(x, 2)rather than-1 / x^2 - if ported, this should follow Java exactly unless the source of truth is corrected first
- proposed C files:
src/Functions/Mask.hsrc/Functions/Mask.c
- likely
ComputationalGraph-Cdependency:Function.hTensor.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- simple upper-triangular masking with
-inf - likely useful early for transformer work
- simple upper-triangular masking with
- proposed C files:
src/Functions/Mean.hsrc/Functions/Mean.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- row-wise reduction then broadcast back to original shape
- straightforward derivative mask of
1 / width
- proposed C files:
src/Functions/MultiplyByConstant.hsrc/Functions/MultiplyByConstant.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- easy stateful unary transform
- proposed C files:
src/Functions/RemoveBias.hsrc/Functions/RemoveBias.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- shape-sensitive but still simple
- trims the last element and re-expands gradient with a zero appended
- proposed C files:
src/Functions/SquareRoot.hsrc/Functions/SquareRoot.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- stateful through
epsilon - derivative in Java uses
1 / (2 * val)wherevalis read from input tensor, not fromsqrt(epsilon + x)output - if ported now, it should mirror Java as written
- stateful through
- proposed C files:
src/Functions/Switch.hsrc/Functions/Switch.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- mutable runtime state through
turn - returns input unchanged or a same-shaped zero tensor
- derivative mirrors the same switch behavior
- mutable runtime state through
- proposed C files:
src/Functions/Transpose.hsrc/Functions/Transpose.c
- likely
ComputationalGraph-Cdependency:Function.hMath-C: transpose_tensor(...)- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- easiest candidate because
Math-Calready hastranspose_tensor
- easiest candidate because
- proposed C files:
src/Functions/Variance.hsrc/Functions/Variance.c
- likely
ComputationalGraph-Cdependency:Function.h- later local edge helper
- can be implemented locally in
SequenceProcessing-C:- yes
- requires missing base abstraction in
ComputationalGraph-C:- no for math
- yes for Java-style
addEdge(...)
- notes:
- derivative in Java appears mathematically suspicious
- highest review risk among current function classes
The following are not blocked for pure tensor-math porting:
AdditionByConstantMaskMeanMultiplyByConstantRemoveBiasSwitchTransposeInverseSquareRootVariance
Reason:
ComputationalGraph-Calready has a function-vtable modelMath-Calready has enough tensor operations for unary transforms, hadamard products, transpose, and shape-preserving tensor allocation
What is blocked is full Java-class parity for the graph-construction method:
addEdge(ArrayList<ComputationalNode> inputNodes, boolean isBiased)
Reason:
- Java function objects mutate node connectivity directly
- C graph construction currently requires
Computational_graph_ptrand uses graph-owned edge maps - there is no direct
FunctionNodeor node-local adjacency abstraction inComputationalGraph-C
The missing abstraction is not “function math”. The missing abstraction is “function-local graph attachment without an explicit graph handle”.
Minimal local fix in SequenceProcessing-C:
- add one small helper to bridge from
(graph, input node, function, is_biased)- to
ComputationalGraph-C:add_edge(...)
This is enough to unblock the function layer without forking ComputationalGraph-C.
TransposeAdditionByConstantMultiplyByConstantRemoveBiasMaskMeanSwitch
InverseSquareRoot
Variance
Risk rationale:
Transposeis mostly a direct wrapper over existing tensor support- constant transforms and
RemoveBiasare shape-simple MaskandMeanneed more explicit shape loops but are still straightforwardSwitchadds mutable stateInverse,SquareRoot, and especiallyVariancehave derivative formulas that should be reviewed carefully against the Java source before freezing C behavior
- implement local SequenceProcessing function structs that embed
Function - do not try to recreate Java
FunctionNode - add a small local graph-edge helper in
SequenceProcessing-Cwhen model ports start needing these functions in graphs
As pure function math:
TransposeAdditionByConstantMultiplyByConstantRemoveBiasMaskMeanSwitch
Possible now but with derivative-review caution:
InverseSquareRootVariance
Blocked only for full Java addEdge(...) parity:
- all ten functions
because the blocking issue is shared graph-attachment architecture, not per-function tensor math.
B. patch architecture in SequenceProcessing-C
Reason:
ComputationalGraph-Calready has enough low-level primitives for function execution and graph insertion- the missing piece is a local compatibility layer for graph edge creation
- moving abstractions into
ComputationalGraph-Ccan wait until there is a stronger need for Java-style API parity across multiple repos