Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(deepdoc): disable ORT BFC memory arena for pooled inference sessions (#19800)

xugangqiang2d0757a99730
diff --git a/internal/deepdoc/native/ocr_rec.go b/internal/deepdoc/native/ocr_rec.go
index 05daa459d9..16e604b9b9 100644
--- a/internal/deepdoc/native/ocr_rec.go
+++ b/internal/deepdoc/native/ocr_rec.go
@@ -318,6 +318,16 @@ func newRecSession(modelPath, inName string, inShape []int64, outName string) (*
inT.Destroy()
return nil, err
}
+ // Disable the BFC memory arena for this session. See the matching comment in
+ // NewSession (session.go): the arena never shrinks, so pooled rec sessions
+ // (up to recMaxShapePools*recShapePoolCap = 128 idle) each hoard a native
+ // block, which drove most of the ~14 GB of native memory at the ~20 GB OOM
+ // peak. With the arena off, idle rec sessions keep only their weights.
+ if err := opts.SetCpuMemArena(false); err != nil {
+ opts.Destroy()
+ inT.Destroy()
+ return nil, err
+ }
sess, err := ort.NewDynamicAdvancedSession(modelPath,
[]string{inName}, []string{outName}, opts)
if err != nil {
diff --git a/internal/deepdoc/native/session.go b/internal/deepdoc/native/session.go
index c50e7057ee..666cb54ec0 100644
--- a/internal/deepdoc/native/session.go
+++ b/internal/deepdoc/native/session.go
@@ -122,6 +122,19 @@ func NewSession(modelPath, inName string, inShape []int64, outName string, outSh
outT.Destroy()
return nil, err
}
+ // Disable the BFC memory arena for this session. The arena pre-reserves a
+ // native block per session and never shrinks it, so every pooled (often
+ // idle) session hoards one. Across the rec/det/DLA/TSR pools (~220 live
+ // sessions while parsing a large PDF) this dominated the ~14 GB of native
+ // memory seen at the ~20 GB OOM peak. With the arena off, idle sessions keep
+ // only their weights; activation tensors are allocated directly and freed
+ // after each Run.
+ if err := opts.SetCpuMemArena(false); err != nil {
+ opts.Destroy()
+ inT.Destroy()
+ outT.Destroy()
+ return nil, err
+ }
sess, err := ort.NewAdvancedSession(modelPath,
[]string{inName}, []string{outName},
[]ort.Value{inT}, []ort.Value{outT}, opts)