@@ -163,66 +163,65 @@ void test_linear_backward() {
163163 }
164164 }
165165
166- // TODO: Tensor_sum and Tensor_mean backward is in working progress
167- // // Test Case 4: Chained operations with linear
168- // {
169- // const char* tc_name = "Chained_operations_with_linear";
170- // // Sub-test 1: Linear followed by sum
171- // {
172- // TensorShape input_shape = {2, 3}; // batch_size=2, input_features=3
173- // TensorShape weight_shape = {3, 2}; // input_features=3, output_features=2
174- // TensorShape bias_shape = {1, 2}; // output_features=2
166+ // Test Case 4: Chained operations with linear
167+ {
168+ const char * tc_name = "Chained_operations_with_linear" ;
169+ // Sub-test 1: Linear followed by sum
170+ {
171+ TensorShape input_shape = {2 , 3 }; // batch_size=2, input_features=3
172+ TensorShape weight_shape = {3 , 2 }; // input_features=3, output_features=2
173+ TensorShape bias_shape = {1 , 2 }; // output_features=2
175174
176- // float input_data[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
177- // float weight_data[] = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
178- // float bias_data[] = {0.1f, 0.2f};
175+ float input_data [] = {1.0f , 2.0f , 3.0f , 4.0f , 5.0f , 6.0f };
176+ float weight_data [] = {0.1f , 0.2f , 0.3f , 0.4f , 0.5f , 0.6f };
177+ float bias_data [] = {0.1f , 0.2f };
179178
180- // // Expected gradients
181- // float exp_grad_bias[] = {1 .0f, 1 .0f}; // For sum reduction
179+ // Expected gradients
180+ float exp_grad_bias [] = {2 .0f , 2 .0f }; // For sum reduction
182181
183- // Tensor input = create_test_tensor(input_shape, input_data, true);
184- // Tensor weight = create_test_tensor(weight_shape, weight_data, true);
185- // Tensor bias = create_test_tensor(bias_shape, bias_data, true);
182+ Tensor input = create_test_tensor (input_shape , input_data , true);
183+ Tensor weight = create_test_tensor (weight_shape , weight_data , true);
184+ Tensor bias = create_test_tensor (bias_shape , bias_data , true);
186185
187- // Tensor output = nn_linear(input, weight, bias);
188- // Tensor sum_output = Tensor_sum(output);
186+ Tensor output = nn_linear (input , weight , bias );
187+ Tensor sum_output = Tensor_sum (output );
189188
190- // Tensor_backward(sum_output, (Tensor){0});
189+ Tensor_backward (sum_output , (Tensor ){0 });
191190
192- // Tensor expected_grad_bias = create_test_tensor(bias_shape, exp_grad_bias, false);
191+ Tensor expected_grad_bias = create_test_tensor (bias_shape , exp_grad_bias , false);
193192
194- // // Focus on bias gradient
195- // compare_tensors(&bias.node->grad, &expected_grad_bias, op_name, tc_name, 1, TEST_FLOAT_TOLERANCE);
196- // }
193+ // Focus on bias gradient
194+ compare_tensors (& bias .node -> grad , & expected_grad_bias , op_name , tc_name , 1 , TEST_FLOAT_TOLERANCE );
195+ }
197196
198- // // Sub-test 2: Linear followed by mean
199- // {
200- // TensorShape input_shape = {2, 3}; // batch_size=2, input_features=3
201- // TensorShape weight_shape = {3, 2}; // input_features=3, output_features=2
202- // TensorShape bias_shape = {1, 2}; // output_features=2
197+ // Sub-test 2: Linear followed by mean
198+ {
199+ TensorShape input_shape = {2 , 3 }; // batch_size=2, input_features=3
200+ TensorShape weight_shape = {3 , 2 }; // input_features=3, output_features=2
201+ TensorShape bias_shape = {1 , 2 }; // output_features=2
203202
204- // float input_data[] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
205- // float weight_data[] = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
206- // float bias_data[] = {0.1f, 0.2f};
203+ float input_data [] = {1.0f , 2.0f , 3.0f , 4.0f , 5.0f , 6.0f };
204+ float weight_data [] = {0.1f , 0.2f , 0.3f , 0.4f , 0.5f , 0.6f };
205+ float bias_data [] = {0.1f , 0.2f };
207206
208- // // Expected gradients
209- // float exp_grad_bias[] = {0.25f , 0.25f }; // For mean reduction (1/4 )
207+ // Expected gradients
208+ float exp_grad_bias [] = {0.5f , 0.5f }; // For mean reduction (1/2 )
210209
211- // Tensor input = create_test_tensor(input_shape, input_data, true);
212- // Tensor weight = create_test_tensor(weight_shape, weight_data, true);
213- // Tensor bias = create_test_tensor(bias_shape, bias_data, true);
210+ Tensor input = create_test_tensor (input_shape , input_data , true);
211+ Tensor weight = create_test_tensor (weight_shape , weight_data , true);
212+ Tensor bias = create_test_tensor (bias_shape , bias_data , true);
214213
215- // Tensor output = nn_linear(input, weight, bias);
216- // Tensor mean_output = Tensor_mean(output);
214+ Tensor output = nn_linear (input , weight , bias );
215+ Tensor mean_output = Tensor_mean (output );
217216
218- // Tensor_backward(mean_output, (Tensor){0});
217+ Tensor_backward (mean_output , (Tensor ){0 });
219218
220- // Tensor expected_grad_bias = create_test_tensor(bias_shape, exp_grad_bias, false);
219+ Tensor expected_grad_bias = create_test_tensor (bias_shape , exp_grad_bias , false);
221220
222- // // Focus on bias gradient
223- // compare_tensors(&bias.node->grad, &expected_grad_bias, op_name, tc_name, 2, TEST_FLOAT_TOLERANCE);
224- // }
225- // }
221+ // Focus on bias gradient
222+ compare_tensors (& bias .node -> grad , & expected_grad_bias , op_name , tc_name , 2 , TEST_FLOAT_TOLERANCE );
223+ }
224+ }
226225
227226 cten_free (pool_id );
228227}
0 commit comments