問題文
A script must run unchanged on a workstation with a CUDA GPU and on a laptop with no accelerator. Which pattern for choosing the device is appropriate?
選択肢
- Set an environment variable and let the process decide.
- Hard-code the GPU device and catch the exception if it is missing.
- Query availability at startup and build a device object from the result, then pass that device to every tensor and module.
- Always construct tensors on the CPU and rely on the framework to migrate them to an accelerator at runtime.