Skip to content

Fixed type instability in tensor operations#1293

Merged
JordiManyer merged 2 commits intomasterfrom
bugfix-tensor-type-instability
May 1, 2026
Merged

Fixed type instability in tensor operations#1293
JordiManyer merged 2 commits intomasterfrom
bugfix-tensor-type-instability

Conversation

@JordiManyer
Copy link
Copy Markdown
Member

The problem:

Every arithmetic operation on MultiValue types (scalar *, +, -, /) followed the same pattern: extract the independent components as a tuple, apply the operation element-wise, reconstruct
the result. The extraction was written as Tuple(a)[1:Li] where Li = num_indep_components(a) is a plain Int computed at runtime.

The problem is that slicing a tuple with a runtime integer is type-unstable in Julia: the compiler sees NTuple{N,T}[1:Int] and cannot determine the length of the result statically. Everything downstream — the element type, the result type, the constructor call — then cascades into uncertainty. Thus, Base.promote_op cannot infer the output type and returns Any.

The reason we slice instead of just taking the whole data is that some types (only SymTracelessTensorValue in reality) store extra components that are not independent. So the [1:Li] was load-bearing for that type specifically, while being a unnecessary for all others.

The fix:

We introduced get_indep_components(a), which replaces the type-unstable slice: It is equivalent to Tuple for all MultiValues, except for the special ones.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 1, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.83%. Comparing base (f39eac1) to head (277fe82).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1293      +/-   ##
==========================================
- Coverage   88.83%   88.83%   -0.01%     
==========================================
  Files         227      227              
  Lines       29775    29772       -3     
==========================================
- Hits        26451    26448       -3     
  Misses       3324     3324              
Flag Coverage Δ
drivers 39.95% <100.00%> (-0.01%) ⬇️
extensions 5.12% <57.14%> (-0.01%) ⬇️
unit-adaptivity 40.09% <85.71%> (-0.02%) ⬇️
unit-basics 14.68% <100.00%> (-0.01%) ⬇️
unit-celldata 21.12% <85.71%> (-0.02%) ⬇️
unit-fespaces-1 32.35% <85.71%> (-0.02%) ⬇️
unit-fespaces-2 38.83% <85.71%> (-0.01%) ⬇️
unit-fields 17.57% <100.00%> (-0.02%) ⬇️
unit-geometry 28.77% <71.42%> (-0.02%) ⬇️
unit-multifield 30.87% <85.71%> (-0.02%) ⬇️
unit-odes 28.74% <71.42%> (-0.02%) ⬇️
unit-referencefes 34.24% <100.00%> (-0.02%) ⬇️
unit-visualization 11.90% <71.42%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@JordiManyer JordiManyer changed the title Fixed type instanbility in tensor operations Fixed type instability in tensor operations May 1, 2026
@JordiManyer JordiManyer merged commit ee7ca9b into master May 1, 2026
58 checks passed
@JordiManyer JordiManyer deleted the bugfix-tensor-type-instability branch May 1, 2026 10:53
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.

1 participant