Skip to content

Generic flatten (2d and 3d) #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 16, 2025

Conversation

milancurcic
Copy link
Member

I attempted to make a generic flatten so that the user doesn't need to do flatten2d. It seems like it will work.

In support of Linear2d (#197)

@milancurcic milancurcic added the enhancement New feature or request label Feb 16, 2025
@milancurcic milancurcic mentioned this pull request Feb 16, 2025
@ricor07
Copy link
Collaborator

ricor07 commented Feb 16, 2025

LGTM

@@ -18,13 +18,20 @@ module nf_flatten_layer
integer, allocatable :: input_shape(:)
integer :: output_size

real, allocatable :: gradient(:,:,:)
real, allocatable :: gradient_2d(:,:)
real, allocatable :: gradient_3d(:,:,:)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I thought about that but decided not to make the code even less SOLID

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But here we have a choice between SOLID and less boilerplate, I think I agree that the second one is better

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and, most importantly for me, this approach allows for a unified API (only one flatten() for the user).


procedure :: forward_2d
procedure :: forward_3d
generic :: forward => forward_2d, forward_3d
Copy link
Collaborator

@OneAdder OneAdder Feb 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, just make it one method with assumed-rank input?

  pure module subroutine forward(self, input)
    class(flatten_layer), intent(in out) :: self
    real, intent(in) :: input(..)
    select rank(input)
      rank(2)
        self % output = pack(input, .true.)
      rank(3)
        self % output = pack(input, .true.)
      rank default
        error stop "Unsupported rank of input"
    end select
  end subroutine forward

It will reduce boilerplate a little

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! If it works, let's do it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@OneAdder
Copy link
Collaborator

Thank you! I'll rebase and test it out!

Copy link
Collaborator

@OneAdder OneAdder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything works. Great job!

@milancurcic milancurcic merged commit 4ad75bc into modern-fortran:main Feb 16, 2025
4 checks passed
@milancurcic milancurcic deleted the generic-flatten branch February 16, 2025 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants