Draft
Add tf_xla_always_compile_on_cache_miss flag to bypass XLA compilation heuristics#5
tf_xla_always_compile_on_cache_miss flag to bypass XLA compilation heuristics#5Conversation
… on every cache miss Agent-Logs-Url: https://github.com/fenxcc/tensorflow/sessions/6b1be8ee-19f6-4689-bb62-383d253778a7 Co-authored-by: fenxcc <64964597+fenxcc@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add option to force XLA compilation on cache miss
Add Apr 1, 2026
tf_xla_always_compile_on_cache_miss flag to bypass XLA compilation heuristics
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.
XLA's
DeviceCompilationProfileruses several heuristics to skip compilation on cache misses: megamorphic detection, lazy compilation thresholds, and async ongoing compilation limits. These heuristics can prevent XLA from compiling clusters with highly dynamic shapes, even when full compilation coverage is desired.Changes
flags.h/flags.cc: Addtf_xla_always_compile_on_cache_missbool toBuildXlaOpsPassFlags(defaultfalse). Registered as aTF_XLA_FLAGSentry.device_compilation_profiler.cc: When the flag is set,ShouldCompileClusterbypasses all three skip gates:is_megamorphic == true→ normally returnsfalse)num_ongoing_compilations_ >= kMaxNumOngoingCompilations)current_request_count < compile_threshold)device_compilation_profiler_test.cc: AddShouldCompileClusterAlwaysCompileOnCacheMisstest covering all three bypass paths.Usage
Original prompt
Implement the requested code changes in fenxcc/tensorflow on ref
master, since the current commit has no code modifications yet.Requirements:
tensorflow/compiler/jit/device_compilation_profiler.ccand flags intensorflow/compiler/jit/flags.*) so when the flag is enabled, the profiler/compiler never skips compilation due to megamorphic heuristics, thresholds, or async ongoing compilation limit.Repo: fenxcc/tensorflow
Branch/ref: master
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: ShouldBeMegamorphic函数的作用是什么?
Assistant:
ShouldBeMegamorphic(int64_t compile_count, int64_t execution_count)的作用是:用一个简单的启发式规则判断某个 XLA 编译 cluster(按 function/cluster 统计)是否“可能是 megamorphic(高度形状/签名多态、动态性过强)”,从而在后续决定是否继续触发编译时作为依据。结合
device_compilation_profiler.cc里的调用链,它的具体语义可以概括为:输入含义
compile_count:这个 cluster 到目前为止被编译了多少次。execution_count:这个 cluster 到目前为止被执行了多少次。判定逻辑(启发式)
也就是说,当满足以下两点时返回
true(认为“应当被视为 megamorphic”):compile_count > 10execution_count / compile_count < 50直觉上:如果一个 cluster 反复编译(通常意味着输入形状/签名变化太多导致缓存命中差),但执行次数又没有相应增长,那么继续为它做 XLA 编译往往不划算。
它在系统中的实际效果
RegisterExecutionForCluster()中被使用:execution_count++ShouldBeMegamorphic(...)stats->is_megamorphic = true(“sticky”,永久生效)DeviceCompilationProfiler::ShouldCompileCluster()里,如果is_megamorphic为true,会直接返回false,从而阻止后续继续编译该 cluster(并广播一个 optimization remark:MEGAMORPHIC_FUNCTION)。总结一句话:
ShouldBeMegamorphic用来识别“编译开销很大但收益很低、形状/签名变化过多导致反复编译”的 cluster,并触发“以后不要再编译它”的策略。User: 如果我将 ShouldBeMegamorphic 始终设置为返回 false,那么 xla 能否编译所有cluster并处理所有形状?我想知道是否存在某种驱逐机制?例如,当缓存内容变得过大时,移除一些不常使用的cluster。
Assistant: 把
ShouldBeMegamorphic()永远改成返回false,只能移除“因为被判定为 megamorphic 而不再编译”的那条刹车;它并不意味着:下面分开说明。
1) 改成恒 false 后,XLA 会不会编译“所有 cluster 并处理所有形状”?
不会得到这个保证。
(A) 仍然可能“不编译”的路径
即使 megamorphic 逻辑被禁用,
DeviceCompilationProfiler::ShouldCompileCluster()里仍有其它 gating:DeviceCompileMode::kLazy:要满足阈值(默认kDefaultCompilationThreshold = 2)才会编译(但注意:代码里还有“第一次执行一定编译”的逻辑,见下文)。DeviceCompileMode::kAsync:有并发上限(kMaxNumOngoingCompilations = kNumAsyncDeviceCompilerThreads),ongoing 太多会拒绝继续异步编译。FailOnXlaCompilation():如果配置为禁止编译,DeviceCompiler::CompileImpl()在 entry 还未编译时会直接报错(你贴的device_compiler.h里就有这段逻辑)。另外还有更“硬”的原因:XLA 编译本身可能失败(不支持的 op、布局/设备限制、shape 推导失败等),这不是 megamorphic 逻辑能解决的。
(B) “处理所有形状”的含义:不是“一个可执行文件覆盖所有形状”
你引用的注释已经说明了关键点:
也就是:XLA 需要静态 shape;如果你的输入 shape 不断变化,系统往往会为“每一种 shape signature”生成/缓存一个新的编译结果/可执行文件。把
ShouldBeMegamorphic设为 false,只会让系统更愿意“继续为新 shape 编译”,但不代表“一个 cluster 就能涵盖无限 shape”。因此在高度动态 shape 的场景,结果很可能是:编译次数暴涨 + cache 变大 + 可能 OOM 或性能变差,而不是“神奇地处理所有形状”。
2) 你问的“驱逐机制”:缓存太大时是否会移除不常用 cluster?
就你贴出来的
device_compiler.h代码而言,有非常明确的信号:目前并没有做 eviction 的结构性支持,至少这段实现仍是“TODO”。在
CompileImpl()里有注释(你贴的原文):这通常意味着:当前 DeviceCompilationCache 的并发/锁设计还没为 eviction 准备好,也暗示“现在没有真正的 eviction”。
另外你还能看到一个重要行为:
CompileStrict()结束时会调用cache_->Finalize();,注释说的是释放 cache 持有的XlaComputation引用(即释放 HLO/计算图层面的对象引用),但它不是“把 cache entry 驱逐掉”。entry(含编译结果、可执行文件指针等)仍然会留在 cache 里。所以回答你的问题:
**在你提供的这些代码上下文中,看不到“缓存过大自动驱逐不常用 cluster”的机制。**更像是“增长型缓存”,配合
Finalize()释放部分中间对象占用,但不是 LRU/size-based eviction。3) 如果强行让它编译所有 shape,会发生什么风险?
kMaxNumOngoingCompilations限制而频繁拒绝/排队。DeviceExecutablePersistor,如果 persist 打开,磁盘也可能越来越大。4) 我建议你下一步怎么验证“是否真的没有 eviction”(在 TF 当前 master 上)
你给的两个文件不足以 100% 排除“别处有 eviction”,但可以很快确认:
tensorflow/compiler/jit/device_compilation_cache.h/.cc里搜索:evict,eviction,LRU,max_size,capacity,prune,TrimDeviceCompilationCache::Store/LookupOrCreate是否有 size 上限逻辑This pull request was created from Copilot chat.