Repository navigation
Stop re-hashing node source on every execution (OpenTelemetry, caching) - #1756
Open
Thanatos9404 wants to merge 1 commit into
Open
Thanatos9404 wants to merge 1 commit into
Thanatos9404 wants to merge 1 commit into
Conversation
Graph execution hooks receive a new HamiltonGraph from HamiltonGraph.from_graph on every run, so the cached `version` properties never survive to the next execution. Reading `graph.version` in the OpenTelemetry tracer, or resolving code versions in the caching adapter, therefore hashed the source of every node on every execution. HamiltonNode.version now reuses the hash from a module-level WeakKeyDictionary keyed by the originating function. Each entry records the code object it was computed from (following `__wrapped__`, as inspect.getsource does), so a function whose code is replaced in place, for example by IPython's autoreload, is hashed again. The version values are unchanged.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1714
Graph execution hooks get a new
HamiltonGraphfromHamiltonGraph.from_graphon every run, so thecached_propertyversions never carry over to the next run.OpenTelemetryTracerreadsgraph.versionbefore every execution, so it hashed the source of every node every time..with_cache()pays the same cost:resolve_code_versionsreadsHamiltonNode.from_node(node).versionfor every node on the execution path.Changes
HamiltonNode.versionnow goes through a private helper,_hash_function_source, which keepshash_source_code(fn, strip=True)results in a module-levelweakref.WeakKeyDictionarykeyed by the originating function.inspect.unwrap(fn)(the functioninspect.getsourcereads) and is recomputed when it changes. IPython's autoreload, which the Jupyter how-to recommends, swaps__code__on the existing function; without this check.with_cache()could return results from the old code.hash_source_codeis untouched.Alternatives:
lru_cacheonhash_source_codewould return stale hashes after autoreload, also toh_diskcacheandCacheAdapter, and keeps every function alive.node.Node, or reusing oneHamiltonGraphperFunctionGraph, would also go stale under autoreload, since those objects outlive the reload, so they would need the same check. Keying by function also hashes a function shared by several nodes (@parameterize, or an input and its declaring function) only once.How I tested this
tests/test_graph_types.py: one hash across threeHamiltonNodewrappers; the version follows a replaced__code__and a replaced__wrapped__.tests/plugins/test_h_opentelemetry.py(new): the second traced execution hashes nothing, the first hashes each function once, and node span versions equal the direct hashes.On
mainthe two counting tests fail (assert 3 == 1,assert 8 == 4) and the other three pass. On this branch they pass on Python 3.10, 3.12 and 3.14, as dotests/lifecycleandtests/caching;prek runpasses on the changed files.Hash calls per execution, chain of 500 functions plus one input, 10 runs:
mainOpenTelemetryTracer.with_cache()With the tracer, runs 2 to 10 took 76 to 130 ms on
mainand 9 to 21 ms here (2 to 5 ms without adapters). The first run costs about the same; the graph version is identical.Notes
mainwould hash the new file text.as_dict()["source"]still reads from disk, so the two can then disagree.opentelemetry-sdkis installed: in CI it comes throughmlflowon Python 3.13 and below, and the file is skipped on 3.14. I can addopentelemetry-sdkto thetestgroup if you prefer.Checklist