pii: pf-serve-vulkan keeps the engine's 4096-token MoE chunk; 2048 was slower (#3682)

7279bdc28 gave pf-serve-vulkan PF_MOE_CHUNK=2048 on the strength of a
1.77x speed-up yunwang2442-creator measured on an RX 470 (Windows,
Vulkan). Their re-test on 0.1.1696 found that number came from runs
hours apart: the same version read the same 480 KB text in 67s at 07:44
and 128-135s after 16:00. Interleaved A/B in one session, same text,
4096 was faster every time:
  96 KB in pieces  2048 25.4-25.8s   4096 24.0-24.2s
  24 KB in pieces  2048 6.2-6.5s     4096 6.0s
  96 KB whole      2048 36.1-36.7s   4096 32.7-33.8s
  480 KB whole     2048 175.6s       4096 161.6s
with the same spans either way. On Metal 2048 was ~6% slower already
(7279bdc28's own numbers). So no pf-serve gets a PF_MOE_CHUNK from
magpie now; one the user set is still inherited through os.Environ,
as before.

moeChunk and vulkanMoEChunk are gone. TestVulkanServeGetsMoEChunk is
now TestServeKeepsMoEChunkUnlessUserSets: pf-serve-vulkan sees no
PF_MOE_CHUNK, and a user's 3072 reaches both.
docs/subsystems/gateway-routing.md says the same.

Run at this head (on 90258cf20):
- gofmt -l internal/pii: clean
- go vet -tags nogui ./internal/pii/ for GOOS linux, windows, darwin: ok
- go test -timeout 45m ./internal/pii/: ok (25.2s)
- Broken by hand: with HEAD's serve.go and the new test,
  TestServeKeepsMoEChunkUnlessUserSets/vulkan fails ("moechunk
  pf-serve-vulkan 2048", want no value); green with this serve.go.

Not tried: a Vulkan GPU here (none on this machine); the numbers above
are the reporter's, from their RX 470. Other GPUs aren't measured;
PF_MOE_CHUNK stays the override for one where 2048 is faster.
This commit is contained in:
yetone
2026-10-28 00:50:09 +08:00
parent 90258cf20a
commit 02cbdf5bdc
3 changed files with 13 additions and 32 deletions
File diff suppressed because one or more lines are too long
+6 -25
View File
@@ -228,10 +228,14 @@ func startOne(path string, cpu bool, stop <-chan struct{}) (*helper, error) {
// second, and the first request after an idle spell takes 0.1-0.45s
// more (51-78ms hot either way).
cmd.Env = append(os.Environ(), "GGML_METAL_NO_RESIDENCY=1")
// PF_MOE_CHUNK is left at the engine's default (the window, 4096) on
// every device: on an RX 470 through Vulkan, interleaved A/B in one
// session read the same spans 6-9% faster at 4096 than at 2048 (#3682;
// the 1.77x once measured for 2048 was the machine's own 2x swing
// between runs hours apart), and on an M4 Pro's Metal 2048 was ~6%
// slower. A PF_MOE_CHUNK the user set is inherited through Environ.
if cpu {
cmd.Env = append(cmd.Env, "PF_DEVICE=cpu")
} else if v := moeChunk(path); v != "" {
cmd.Env = append(cmd.Env, "PF_MOE_CHUNK="+v)
}
wait := loadTimeout
if !cpu {
@@ -280,29 +284,6 @@ func startOne(path string, cpu bool, stop <-chan struct{}) (*helper, error) {
return h, nil
}
// vulkanMoEChunk is the MoE's token chunk pf-serve-vulkan runs with
// (privacy-filter.cpp's PF_MOE_CHUNK, src/model.cpp): the expert matmul
// over a window's tokens in pieces of this many, exactly the same answer
// either way. Its own default is the window, 4096. On an RX 470/580-class
// GPU through Vulkan, 2048 read the same texts 1.77x faster at every size
// from 24 KB to 480 KB (#3682: 480 KB in 90s, not 159s), and was the
// fastest of 1024 to 3072. On Metal it is a little slower (an M4 Pro: 96
// KB in 2.00s, 1.87s at 4096), so macOS's pf-serve keeps the default, and
// the CPU's is the window either way.
const vulkanMoEChunk = "2048"
// moeChunk is the PF_MOE_CHUNK magpie gives the GPU's pf-serve at path:
// pf-serve-vulkan's, unless the user set their own, which it inherits.
func moeChunk(path string) string {
if _, set := os.LookupEnv("PF_MOE_CHUNK"); set {
return ""
}
if strings.TrimSuffix(baseName(path), ".exe") == "pf-serve-vulkan" {
return vulkanMoEChunk
}
return ""
}
func baseName(path string) string {
path = strings.ReplaceAll(path, "\\", "/")
return path[strings.LastIndexByte(path, '/')+1:]
+6 -6
View File
@@ -407,18 +407,18 @@ func TestCacheKeepsTheLatest(t *testing.T) {
}
}
// pf-serve-vulkan runs its MoE in 2048-token chunks, 1.77x faster than
// its default 4096 on an RX 470/580-class GPU (#3682); macOS's Metal one
// and the CPU's keep their default. A PF_MOE_CHUNK the user set is theirs,
// on every one.
func TestVulkanServeGetsMoEChunk(t *testing.T) {
// Every pf-serve keeps the engine's own MoE chunk, the window (4096):
// on an RX 470 through Vulkan 4096 was 6-9% faster than 2048 (#3682), on
// Metal 2048 was ~6% slower. A PF_MOE_CHUNK the user set is theirs, on
// every one.
func TestServeKeepsMoEChunkUnlessUserSets(t *testing.T) {
cases := []struct {
name string
vulkan bool
user string // "" unset
want []string
}{
{"vulkan", true, "", []string{"moechunk pf-serve-vulkan 2048", "moechunk pf-serve "}},
{"vulkan", true, "", []string{"moechunk pf-serve-vulkan ", "moechunk pf-serve "}},
{"vulkan, user's own", true, "3072", []string{"moechunk pf-serve-vulkan 3072", "moechunk pf-serve 3072"}},
}
if runtime.GOOS == "darwin" {