Skip to content

Fix torch.compile for timestep_embedding#2

Open
leeclemnet wants to merge 1 commit intomainfrom
fix/compile-timestep-embedder
Open

Fix torch.compile for timestep_embedding#2
leeclemnet wants to merge 1 commit intomainfrom
fix/compile-timestep-embedder

Conversation

@leeclemnet
Copy link

@leeclemnet leeclemnet commented Feb 24, 2026

What does this PR do?

The use of np.log in timestep_embedding functions causes SAM3_3D inference to fail when torch_compile=True.

The reason is that when torch.compile traces this, dynamo sees -np.log(max_period) and tries to negate it through torch_function, which calls .neg() on the numpy float64 result — and numpy scalars don't have .neg(). Switching to math.log avoids the issue by returning a plain Python float.

Associated inference PR: roboflow/inference#2041

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • I have tested this change locally
  • I have added/updated tests for this change <-- no tests in tdfy currently

Test details:

Compilation with torch_compile=True succeeds after ~20min with torch_compile=True, model behaviour is not affected

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in hard-to-understand areas
  • My changes generate no new warnings or errors
  • I have updated the documentation accordingly (if applicable)

Additional Context

@leeclemnet leeclemnet marked this pull request as ready for review February 25, 2026 20:44
@leeclemnet leeclemnet requested a review from Matvezy February 25, 2026 20:44
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.

3 participants