@ -237,7 +237,11 @@ std::map<std::string, torch::Tensor> ResNetImpl::forward(torch::Tensor x) {
return std : : find ( _output_layers . begin ( ) , _output_layers . end ( ) , layer_name ) ! = _output_layers . end ( ) ;
return std : : find ( _output_layers . begin ( ) , _output_layers . end ( ) , layer_name ) ! = _output_layers . end ( ) ;
} ;
} ;
// Print input shape
if ( x . size ( 0 ) > 0 ) std : : cout < < " [DEBUG] Input shape: " < < x . sizes ( ) < < std : : endl ;
x = conv1 - > forward ( x ) ;
x = conv1 - > forward ( x ) ;
if ( x . size ( 0 ) > 0 ) torch : : save ( x [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_conv1.pt " ) ;
if ( should_output ( " debug_resnet_conv1_output_for_bn1_input " ) ) {
if ( should_output ( " debug_resnet_conv1_output_for_bn1_input " ) ) {
outputs [ " debug_resnet_conv1_output_for_bn1_input " ] = x . clone ( ) ;
outputs [ " debug_resnet_conv1_output_for_bn1_input " ] = x . clone ( ) ;
}
}
@ -247,30 +251,27 @@ std::map<std::string, torch::Tensor> ResNetImpl::forward(torch::Tensor x) {
if ( bn1 ) {
if ( bn1 ) {
x = bn1 - > forward ( x ) ;
x = bn1 - > forward ( x ) ;
}
}
if ( x . size ( 0 ) > 0 ) torch : : save ( x [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_bn1.pt " ) ;
// End apply bn1
// End apply bn1
if ( should_output ( " bn1_output " ) ) outputs [ " bn1_output " ] = x ;
if ( should_output ( " bn1_output " ) ) outputs [ " bn1_output " ] = x ;
x = relu - > forward ( x ) ;
x = relu - > forward ( x ) ;
if ( should_output ( " relu1_output " ) ) outputs [ " relu1_output " ] = x ;
if ( x . size ( 0 ) > 0 ) torch : : save ( x [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_relu1.pt " ) ;
// Save conv1_output AFTER bn1 and relu (matching Python behavior)
// Save conv1_output AFTER bn1 and relu (matching Python behavior)
if ( should_output ( " conv1_output " ) ) outputs [ " conv1_output " ] = x ;
if ( should_output ( " conv1_output " ) ) outputs [ " conv1_output " ] = x ;
torch : : Tensor x_pre_layer1 = maxpool - > forward ( x ) ;
torch : : Tensor x_pre_layer1 = maxpool - > forward ( x ) ;
if ( should_output ( " maxpool_output " ) ) outputs [ " maxpool_output " ] = x_pre_layer1 ;
if ( x_pre_layer1 . size ( 0 ) > 0 ) torch : : save ( x_pre_layer1 [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_maxpool.pt " ) ;
// Save output of layer1.0 block if requested
// Save output of layer1.0 block if requested
if ( should_output ( " layer1_0_block_output " ) ) {
if ( should_output ( " layer1_0_block_output " ) ) {
if ( layer1 & & ! layer1 - > is_empty ( ) ) {
if ( layer1 & & ! layer1 - > is_empty ( ) ) {
try {
try {
// Get the base module pointer
std : : shared_ptr < torch : : nn : : Module > base_module_ptr = layer1 - > ptr ( 0 ) ;
std : : shared_ptr < torch : : nn : : Module > base_module_ptr = layer1 - > ptr ( 0 ) ;
// Try to cast it to our BottleneckImpl (which is a torch::nn::Module)
auto bottleneck_impl_ptr = std : : dynamic_pointer_cast < cimp : : resnet : : BottleneckImpl > ( base_module_ptr ) ;
auto bottleneck_impl_ptr = std : : dynamic_pointer_cast < cimp : : resnet : : BottleneckImpl > ( base_module_ptr ) ;
if ( bottleneck_impl_ptr ) {
if ( bottleneck_impl_ptr ) {
// Now call forward on the BottleneckImpl instance
outputs [ " layer1_0_block_output " ] = bottleneck_impl_ptr - > forward ( x_pre_layer1 ) ;
outputs [ " layer1_0_block_output " ] = bottleneck_impl_ptr - > forward ( x_pre_layer1 ) ;
} else {
} else {
std : : cerr < < " ERROR: layer1->ptr(0) could not be dynamically cast to BottleneckImpl! Module type: "
std : : cerr < < " ERROR: layer1->ptr(0) could not be dynamically cast to BottleneckImpl! Module type: "
@ -283,6 +284,7 @@ std::map<std::string, torch::Tensor> ResNetImpl::forward(torch::Tensor x) {
}
}
torch : : Tensor x_after_layer1 = layer1 - > forward ( x_pre_layer1 ) ;
torch : : Tensor x_after_layer1 = layer1 - > forward ( x_pre_layer1 ) ;
if ( x_after_layer1 . size ( 0 ) > 0 ) torch : : save ( x_after_layer1 [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_layer1.pt " ) ;
if ( should_output ( " layer1 " ) ) outputs [ " layer1 " ] = x_after_layer1 ;
if ( should_output ( " layer1 " ) ) outputs [ " layer1 " ] = x_after_layer1 ;
if ( should_output ( " layer1_0_shortcut_output " ) ) {
if ( should_output ( " layer1_0_shortcut_output " ) ) {
@ -290,7 +292,6 @@ std::map<std::string, torch::Tensor> ResNetImpl::forward(torch::Tensor x) {
try {
try {
std : : shared_ptr < torch : : nn : : Module > first_block_module_ptr = layer1 - > ptr ( 0 ) ;
std : : shared_ptr < torch : : nn : : Module > first_block_module_ptr = layer1 - > ptr ( 0 ) ;
auto bottleneck_module_holder = std : : dynamic_pointer_cast < cimp : : resnet : : BottleneckImpl > ( first_block_module_ptr ) ;
auto bottleneck_module_holder = std : : dynamic_pointer_cast < cimp : : resnet : : BottleneckImpl > ( first_block_module_ptr ) ;
if ( bottleneck_module_holder ) {
if ( bottleneck_module_holder ) {
if ( bottleneck_module_holder - > projection_shortcut ) {
if ( bottleneck_module_holder - > projection_shortcut ) {
torch : : Tensor shortcut_out = bottleneck_module_holder - > projection_shortcut - > forward ( x_pre_layer1 ) ;
torch : : Tensor shortcut_out = bottleneck_module_holder - > projection_shortcut - > forward ( x_pre_layer1 ) ;
@ -306,12 +307,15 @@ std::map<std::string, torch::Tensor> ResNetImpl::forward(torch::Tensor x) {
torch : : Tensor x_current = x_after_layer1 ;
torch : : Tensor x_current = x_after_layer1 ;
x_current = layer2 - > forward ( x_current ) ;
x_current = layer2 - > forward ( x_current ) ;
if ( x_current . size ( 0 ) > 0 ) torch : : save ( x_current [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_layer2.pt " ) ;
if ( should_output ( " layer2 " ) ) outputs [ " layer2 " ] = x_current ;
if ( should_output ( " layer2 " ) ) outputs [ " layer2 " ] = x_current ;
x_current = layer3 - > forward ( x_current ) ;
x_current = layer3 - > forward ( x_current ) ;
if ( x_current . size ( 0 ) > 0 ) torch : : save ( x_current [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_layer3.pt " ) ;
if ( should_output ( " layer3 " ) ) outputs [ " layer3 " ] = x_current ;
if ( should_output ( " layer3 " ) ) outputs [ " layer3 " ] = x_current ;
x_current = layer4 - > forward ( x_current ) ;
x_current = layer4 - > forward ( x_current ) ;
if ( x_current . size ( 0 ) > 0 ) torch : : save ( x_current [ 0 ] . cpu ( ) , " test/output/resnet_debug/sample_0_after_layer4.pt " ) ;
if ( should_output ( " layer4 " ) ) outputs [ " layer4 " ] = x_current ;
if ( should_output ( " layer4 " ) ) outputs [ " layer4 " ] = x_current ;
if ( should_output ( " features " ) ) outputs [ " features " ] = x_current ;
if ( should_output ( " features " ) ) outputs [ " features " ] = x_current ;