Skip to content

Determine sparse value write using a structural zeros flag - #96

Merged
drhagen merged 7 commits into
masterfrom
structural-zeros-flag
Aug 9, 2026
Merged

drhagen merged 7 commits into
masterfrom
structural-zeros-flag

Conversation

@drhagen

@drhagen drhagen commented Aug 9, 2026

Copy link
Copy Markdown
Owner

We currently determine that a sparse output layer needs to write a value using two rules:

  1. If this is dense iteration, then write an explicit zero.
    • This is very problematic because this results in a large number of unnecessary explicit zeros when this layer is on top of a sparse input layer, such as a contraction, which could be empty and should be stored as an implicit zero.
  2. If the next layer is sparse, write a value if and only if that lower layer wrote a value.
    • This is not wrong, and is fine.
  3. If the next layer is dense, write a value because a dense layer has to write a value.
    • This is wildly wrong for the same reason as (1). A sparse layer even lower down could be empty.

This PR changes the heuristic entirely. A sparse layer writes a value at the end of an iteration if and only if a terminal expression wrote a value during that iteration. This is tracked by having a written_* flag for each sparse output layer. This flag is set to false at the start of each iteration corresponding to that layer, and all flags are set to true every time a value is written.

This replaces the previous mechanism of a sparse layer looking at the layer below it to see if it advanced. We might be leaving a bit of performance on the table by setting all the flags every time. We could probably be a little more efficient if we only set the upper layer's flag when we handled a lower layer's flag rather than setting it on every terminal evaluation.

There is also one more interesting fact. A terminal expression that writes a literal 0 does not set the flag. A sparse layer above a dense layer should not write a value if only zeros were written to the dense layer. There may be some performance gains to be had here also, but these kernels are pretty rare, so they will probably not be a priority for a while.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 97.87234% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.82%. Comparing base (82498b8) to head (0b4cfcd).

Files with missing lines Patch % Lines
src/tensora/iteration_graph/_generate_ir.py 93.33% 0 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #96      +/-   ##
==========================================
+ Coverage   90.57%   90.82%   +0.25%     
==========================================
  Files          60       60              
  Lines        3151     3161      +10     
  Branches      384      386       +2     
==========================================
+ Hits         2854     2871      +17     
+ Misses        215      208       -7     
  Partials       82       82              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@drhagen
drhagen merged commit acfed6f into master Aug 9, 2026
22 checks passed
@drhagen
drhagen deleted the structural-zeros-flag branch August 9, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants