Skip to content

Fix: Implement dynamic shape for Flatten layer to replace hardcoded fallback #535

@jtaeyeon05

Description

@jtaeyeon05

🧠 D: DEFINE — Problem & Opportunity

What is the problem, limitation, or opportunity? Why does this matter for SKaiNET?

Currently, the flatten() layer in NeuralNetworkDslImpl and StageImpl lacks dynamic shape inference. Instead, it relies on a hardcoded fallback (lastDimension = 1568) meant for a specific MNIST test case. This causes severe ArrayIndexOutOfBoundsException errors when users build custom architectures (e.g., a 64-channel CNN), as the framework miscalculates the input size for subsequent dense layers.


🔍 A: ASSESS — Feasibility & Impact

Provide an evaluation of the proposal. Address the following:

✔️ Feasibility

Straightforward but requires updating all spatial layers (Conv, Pool) in the DSL to calculate and update their output shapes mathematically before passing them to the next layer.

✔️ Expected Impact

Critical fix: Unlocks the ability to build and run custom CNN architectures without hardcoded shape collisions.

✔️ Risks / Constraints

Need to ensure shape calculation formulas (handling stride, padding, dilation) exactly match the tensor operations.

✔️ Dependencies

Core sk.ainet.lang.nn.dsl module.


📚 R: RESEARCH — What Must Be Understood First?

Document research tasks or open questions that must be answered before implementation:

Research Tasks

  • Identify the bug location (flatten() TODO block).

🛠️ C: CONTRIBUTE — Implementation Plan

Break down actionable steps required to deliver this feature:

Development Tasks

  • Refactor DSL builders to track multidimensional shapes (not just a flat lastDimension integer).
  • Add shape calculation logic to conv2d, maxPool2d, avgPool2d, etc.
  • Remove the hardcoded 1568 fallback in FlattenImpl.
  • Write unit tests verifying shape inference for various CNN architectures.

Acceptance Criteria

  • Feature works as intended and passes all tests
  • Fully documented with clear examples
  • Follows SKaiNET coding style & modular design
  • Reviewed and approved by maintainers
  • No regressions introduced elsewhere

💬 Additional Notes

  • Bug Lotations: sk.ainet.lang.nn.dsl.NetworkBuilder 1077, 1431

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions