2323 vjp_from_jacobian ,
2424)
2525
26- # A fixed 2×3 linear map: f(x) = A @ x, Jacobian is A everywhere.
26+ # A fixed 2x3 linear map: f(x) = A @ x, Jacobian is A everywhere.
2727_A = np .array ([[1.0 , 2.0 , 3.0 ], [4.0 , 5.0 , 6.0 ]], dtype = np .float32 )
2828
2929_linear_input = {"x" : np .array ([1.0 , 0.0 , 0.0 ], dtype = np .float32 )}
@@ -70,18 +70,28 @@ def apply(self, inputs):
7070 def jacobian (self , inputs , jac_inputs , jac_outputs ):
7171 return {"y" : {"x" : _A .copy ()}}
7272
73- def jacobian_vector_product (self , inputs , jvp_inputs , jvp_outputs , tangent_vector ):
74- return jvp_from_jacobian (self .jacobian , inputs , jvp_inputs , jvp_outputs , tangent_vector )
73+ def jacobian_vector_product (
74+ self , inputs , jvp_inputs , jvp_outputs , tangent_vector
75+ ):
76+ return jvp_from_jacobian (
77+ self .jacobian , inputs , jvp_inputs , jvp_outputs , tangent_vector
78+ )
7579
76- def vector_jacobian_product (self , inputs , vjp_inputs , vjp_outputs , cotangent_vector ):
77- return vjp_from_jacobian (self .jacobian , inputs , vjp_inputs , vjp_outputs , cotangent_vector )
80+ def vector_jacobian_product (
81+ self , inputs , vjp_inputs , vjp_outputs , cotangent_vector
82+ ):
83+ return vjp_from_jacobian (
84+ self .jacobian , inputs , vjp_inputs , vjp_outputs , cotangent_vector
85+ )
7886
7987 return LinearModule ("LinearModule" )
8088
8189
8290def test_jvp_via_experimental_helper (module_jac_with_derived_jvp_vjp ):
8391 endpoints = create_endpoints (module_jac_with_derived_jvp_vjp )
84- endpoint_func , EndpointSchema , _ = _find_endpoint (endpoints , "jacobian_vector_product" )
92+ endpoint_func , EndpointSchema , _ = _find_endpoint (
93+ endpoints , "jacobian_vector_product"
94+ )
8595
8696 payload = EndpointSchema .model_validate (
8797 {
@@ -98,7 +108,9 @@ def test_jvp_via_experimental_helper(module_jac_with_derived_jvp_vjp):
98108
99109def test_vjp_via_experimental_helper (module_jac_with_derived_jvp_vjp ):
100110 endpoints = create_endpoints (module_jac_with_derived_jvp_vjp )
101- endpoint_func , EndpointSchema , _ = _find_endpoint (endpoints , "vector_jacobian_product" )
111+ endpoint_func , EndpointSchema , _ = _find_endpoint (
112+ endpoints , "vector_jacobian_product"
113+ )
102114
103115 payload = EndpointSchema .model_validate (
104116 {
@@ -140,19 +152,27 @@ def OutputSchema(self):
140152 def apply (self , inputs ):
141153 return _OutputSchema (y = _A @ np .asarray (inputs .x ))
142154
143- def jacobian_vector_product (self , inputs , jvp_inputs , jvp_outputs , tangent_vector ):
155+ def jacobian_vector_product (
156+ self , inputs , jvp_inputs , jvp_outputs , tangent_vector
157+ ):
144158 t = np .asarray (tangent_vector ["x" ])
145159 return {"y" : _A @ t }
146160
147161 def jacobian (self , inputs , jac_inputs , jac_outputs ):
148162 return jacobian_from_jvp (
149- self .jacobian_vector_product , self .apply , inputs , jac_inputs , jac_outputs
163+ self .jacobian_vector_product ,
164+ self .apply ,
165+ inputs ,
166+ jac_inputs ,
167+ jac_outputs ,
150168 )
151169
152170 return LinearModule ("LinearModule" )
153171
154172
155- def test_jacobian_derived_from_jvp_via_experimental_helper (module_jvp_with_derived_jacobian ):
173+ def test_jacobian_derived_from_jvp_via_experimental_helper (
174+ module_jvp_with_derived_jacobian ,
175+ ):
156176 endpoints = create_endpoints (module_jvp_with_derived_jacobian )
157177 endpoint_func , EndpointSchema , _ = _find_endpoint (endpoints , "jacobian" )
158178
@@ -194,19 +214,27 @@ def OutputSchema(self):
194214 def apply (self , inputs ):
195215 return _OutputSchema (y = _A @ np .asarray (inputs .x ))
196216
197- def vector_jacobian_product (self , inputs , vjp_inputs , vjp_outputs , cotangent_vector ):
217+ def vector_jacobian_product (
218+ self , inputs , vjp_inputs , vjp_outputs , cotangent_vector
219+ ):
198220 v = np .asarray (cotangent_vector ["y" ])
199221 return {"x" : _A .T @ v }
200222
201223 def jacobian (self , inputs , jac_inputs , jac_outputs ):
202224 return jacobian_from_vjp (
203- self .vector_jacobian_product , self .apply , inputs , jac_inputs , jac_outputs
225+ self .vector_jacobian_product ,
226+ self .apply ,
227+ inputs ,
228+ jac_inputs ,
229+ jac_outputs ,
204230 )
205231
206232 return LinearModule ("LinearModule" )
207233
208234
209- def test_jacobian_derived_from_vjp_via_experimental_helper (module_vjp_with_derived_jacobian ):
235+ def test_jacobian_derived_from_vjp_via_experimental_helper (
236+ module_vjp_with_derived_jacobian ,
237+ ):
210238 endpoints = create_endpoints (module_vjp_with_derived_jacobian )
211239 endpoint_func , EndpointSchema , _ = _find_endpoint (endpoints , "jacobian" )
212240
0 commit comments