Skip to content

fix multiple GPUs per node not working with TF - #958

Open
chrisb09 wants to merge 4 commits into
RedisAI:masterfrom
chrisb09:fix/tf-multigpu-default-device
Open

chrisb09 wants to merge 4 commits into
RedisAI:masterfrom
chrisb09:fix/tf-multigpu-default-device

Conversation

@chrisb09

@chrisb09 chrisb09 commented Dec 5, 2026 •

Copy link
Copy Markdown

Pin per-GPU TF models at graph import instead of visible_device_list

TF's process-global GpuIdManager (virtual->physical GPU map) is written once, by the first GPU session in the process, and never updated. The per-session gpu_options.visible_device_list therefore cannot give different sessions different physical GPUs: hosting more than one per-GPU TF model in one process makes every session after the first come up without any GPU device, surfacing as
'ERR GPU requested but TF couldn't find CUDA'.

Drop visible_device_list (keep allow_growth) and pin each model copy at graph-import time with TF_ImportGraphDefOptionsSetDefaultDevice, which placement honors per model.

Reproduced with SmartSim 0.8 / TF 2.15 / 2x H100; see CrayLabs/SmartRedis#424.


Note

Medium Risk
Changes TensorFlow GPU placement and model-import failure modes for all GPU models; graphs with hard-pinned GPU nodes may now be rejected where they previously loaded incorrectly.

Overview
Fixes TensorFlow models targeting different GPUs in the same process by stopping use of gpu_options.visible_device_list (which only applies a process-global GPU map on the first session) and pinning each imported graph to the requested /GPU:N via TF_ImportGraphDefOptionsSetDefaultDevice, while keeping allow_growth on the session.

Adds RAI_TFParseGpuDeviceIndex and a post-import scan that fails model load if any graph node has an explicit GPU device that disagrees with the requested index, avoiding silent sharing of one GPU across “per-GPU” model copies.

Also validates that a requested GPU index exists when the session sees all CUDA devices, improves the cleanup path by closing/deleting a partially created session, and drops the old indexed visible_device_list protobuf session config.

Reviewed by Cursor Bugbot for commit c80ddb8. Bugbot is set up for automated code reviews on this repo. Configure here.

TF's process-global GpuIdManager (virtual->physical GPU map) is written
once, by the first GPU session in the process, and never updated. The
per-session gpu_options.visible_device_list therefore cannot give
different sessions different physical GPUs: hosting more than one
per-GPU TF model in one process makes every session after the first
come up without any GPU device, surfacing as
'ERR GPU requested but TF couldn't find CUDA'.

Drop visible_device_list (keep allow_growth) and pin each model copy at
graph-import time with TF_ImportGraphDefOptionsSetDefaultDevice, which
placement honors per model.

Reproduced with SmartSim 0.8 / TF 2.15 / 2x H100; see CrayLabs/SmartRedis#424.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread src/backends/tensorflow.c Resolved
Comment thread src/backends/tensorflow.c Resolved
Now that the session config no longer sets gpu_options.visible_device_list,
every session sees all GPUs of the process, and the pre-existing CUDA
check only proves that some GPU exists. An out-of-range index such as
GPU:3 on a 2-GPU host would therefore pass AI.MODELSTORE and fail later,
at AI.MODELEXECUTE, with a TensorFlow placement error.

Count the GPUs returned by TF_SessionListDevices and reject
deviceid >= num_gpus up front, restoring store-time validation with a
message that names the actual problem instead of the old (accidental)
'ERR GPU requested but TF couldn't find CUDA'.

Addresses the review finding on PR RedisAI#958.
chrisb09 added a commit to chrisb09/redis-inference-optimization that referenced this pull request Dec 5, 2026
Now that the session config no longer sets gpu_options.visible_device_list,
every session sees all GPUs of the process, and the pre-existing CUDA
check only proves that some GPU exists. An out-of-range index such as
GPU:3 on a 2-GPU host would therefore pass AI.MODELSTORE and fail later,
at AI.MODELEXECUTE, with a TensorFlow placement error.

Count the GPUs returned by TF_SessionListDevices and reject
deviceid >= num_gpus up front, restoring store-time validation with a
message that names the actual problem instead of the old (accidental)
'ERR GPU requested but TF couldn't find CUDA'.

Addresses the review finding on PR RedisAI#958 (v1.2.7 backport).
…ed GPU

TF_ImportGraphDefOptionsSetDefaultDevice only fills nodes that carry no
explicit device annotation. A node annotated /GPU:0 keeps its annotation
when the model copy is stored as GPU:1, and TF placement silently sends
it to that GPU's device 0, so per-GPU model copies would share a GPU
without any error.

After importing an indexed-GPU model, scan the graph with
TF_GraphNextOperation/TF_OperationDevice and fail at model creation if
any node is annotated with a different GPU index. The check accepts
both the modern ('/device:GPU:1') and the TF1-era lowercase ('/gpu:0')
device spellings, case-insensitively.

Graphs without node-level annotations (the freeze_graph/SavedModel
default) are unaffected.

Addresses the remaining review finding on PR RedisAI#958.
chrisb09 added a commit to chrisb09/redis-inference-optimization that referenced this pull request Dec 5, 2026
…ed GPU

TF_ImportGraphDefOptionsSetDefaultDevice only fills nodes that carry no
explicit device annotation. A node annotated /GPU:0 keeps its annotation
when the model copy is stored as GPU:1, and TF placement silently sends
it to that GPU's device 0, so per-GPU model copies would share a GPU
without any error.

After importing an indexed-GPU model, scan the graph with
TF_GraphNextOperation/TF_OperationDevice and fail at model creation if
any node is annotated with a different GPU index. The check accepts
both the modern ('/device:GPU:1') and the TF1-era lowercase ('/gpu:0')
device spellings, case-insensitively.

Graphs without node-level annotations (the freeze_graph/SavedModel
default) are unaffected.

Addresses the remaining review finding on PR RedisAI#958.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread src/backends/tensorflow.c Resolved
Comment thread src/backends/tensorflow.c Resolved
…on leak on error

RAI_TFParseGpuDeviceIndex read p[1]..p[3] on every loop step, which
runs past the terminating null when fewer than 4 characters remain;
bail out early when the lookahead could leave the string.

The cleanup path of RAI_ModelCreateTF never closed the session created
by TF_NewSession, so every model-creation error after that point
(including the pre-existing 'ERR GPU requested but TF couldn't find
CUDA' path) leaked a fully initialized TF session. Close and delete it
on cleanup.

Addresses the two new review findings on PR RedisAI#958.
chrisb09 added a commit to chrisb09/redis-inference-optimization that referenced this pull request Dec 5, 2026
…on leak on error

RAI_TFParseGpuDeviceIndex read p[1]..p[3] on every loop step, which
runs past the terminating null when fewer than 4 characters remain;
bail out early when the lookahead could leave the string.

The cleanup path of RAI_ModelCreateTF never closed the session created
by TF_NewSession, so every model-creation error after that point
(including the pre-existing 'ERR GPU requested but TF couldn't find
CUDA' path) leaked a fully initialized TF session. Close and delete it
on cleanup.

Addresses the two new review findings on PR RedisAI#958.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit c80ddb8. Configure here.

Comment thread src/backends/tensorflow.c
// copies hosted by the same process.
char pinDevice[32];
snprintf(pinDevice, sizeof(pinDevice), "/GPU:%lld", (long long)deviceid);
TF_ImportGraphDefOptionsSetDefaultDevice(options, pinDevice);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GPU pin breaks CPU-only operations

High Severity

TF_ImportGraphDefOptionsSetDefaultDevice writes the requested GPU onto every unannotated node, including ops that only have CPU kernels. The GPU session config still leaves allow_soft_placement at its false default, so those graphs fail at execute time on GPU:N even when they previously ran there.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c80ddb8. Configure here.

Comment thread src/backends/tensorflow.c
RAI_SetError(error, RAI_EMODELIMPORT, msg);
goto cleanup;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function bodies skip GPU isolation

Medium Severity

Both TF_ImportGraphDefOptionsSetDefaultDevice and the post-import conflict scan only touch top-level graph nodes. Nested FunctionDef bodies keep their original devices and are never inspected, so a GPU:1 copy can still run nested compute on GPU:0 without the new isolation error.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c80ddb8. Configure here.

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.

1 participant