{"record":{"id":"5549c33b2699e527","repo":"apache/beam","slug":"invalid-device-value-device-please-specify-either-cpu-or-gpu","errorCode":null,"errorMessage":"Invalid device value: {device}. Please specify either CPU or GPU. Defaults to GPU if no value is provided.","messagePattern":"Invalid device value: (.+?)\\. Please specify either CPU or GPU\\. Defaults to GPU if no value is provided\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/huggingface_inference.py","lineNumber":677,"sourceCode":"        batch_bucket_boundaries=batch_bucket_boundaries,\n        large_model=large_model,\n        model_copies=model_copies,\n        **kwargs)\n    self._task = task\n    self._model = model\n    self._inference_fn = inference_fn\n    self._load_pipeline_args = load_pipeline_args if load_pipeline_args else {}\n    self._framework = \"pt\"\n\n    # Check if the device is specified twice. If true then the device parameter\n    # of model handler is overridden.\n    self._deduplicate_device_value(device)\n    _validate_constructor_args_hf_pipeline(self._task, self._model)\n\n  def _deduplicate_device_value(self, device: Optional[str]):\n    current_device = device.upper() if device else None\n    if (current_device and current_device != 'CPU' and current_device != 'GPU'):\n      raise ValueError(\n          f\"Invalid device value: {device}. Please specify \"\n          \"either CPU or GPU. Defaults to GPU if no value \"\n          \"is provided.\")\n    if 'device' not in self._load_pipeline_args:\n      if current_device == 'CPU':\n        self._load_pipeline_args['device'] = 'cpu'\n      else:\n        if is_gpu_available_torch():\n          self._load_pipeline_args['device'] = 'cuda:0'\n        else:\n          _LOGGER.warning(\n              \"HuggingFaceModelHandler specified a 'GPU' device, \"\n              \"but GPUs are not available. Switching to CPU.\")\n          self._load_pipeline_args['device'] = 'cpu'\n    else:\n      if current_device:\n        raise ValueError(\n            '`device` specified in `load_pipeline_args`. `device` '","sourceCodeStart":659,"sourceCodeEnd":695,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/huggingface_inference.py#L659-L695","documentation":"HuggingFacePipelineModelHandler accepts only 'CPU' or 'GPU' (case-insensitive) for its device parameter. Any other value raises ValueError in _deduplicate_device_value, which normalizes the device into load_pipeline_args.","triggerScenarios":"HuggingFacePipelineModelHandler(device='cuda') or device='tpu' or device='gpu:0'.","commonSituations":"PyTorch-style device strings ('cuda', 'cuda:0') carried over from other handlers; typos like 'gpu ' or 'Cpu ' variants with trailing spaces.","solutions":["Use device='GPU' instead of 'cuda'/'cuda:0'.","Use device='CPU' or omit device (defaults to GPU).","Normalize the value before passing: device = 'GPU' if 'cuda' in raw_device else 'CPU'."],"exampleFix":"// before\nhandler = HuggingFacePipelineModelHandler(task='translation', device='cuda:0')\n// after\nhandler = HuggingFacePipelineModelHandler(task='translation', device='GPU')","handlingStrategy":"type-guard","validationCode":"if device and device.upper() not in ('CPU', 'GPU'):\n    raise ValueError(f'device must be CPU or GPU, got {device}')","typeGuard":"def valid_device(device) -> bool:\n    return device is None or str(device).upper() in ('CPU', 'GPU')","tryCatchPattern":"try:\n    handler = HuggingFacePipelineModelHandler(task=task, device=device)\nexcept ValueError as e:\n    if 'Invalid device value' in str(e):\n        device = 'GPU' if torch.cuda.is_available() else 'CPU'\n        handler = HuggingFacePipelineModelHandler(task=task, device=device)\n    else:\n        raise","preventionTips":["Map torch device strings: 'cuda*' -> 'GPU', else 'CPU'","Remember omitting device defaults to GPU","Strip/case-normalize device strings coming from config"],"tags":["python","apache-beam","machine-learning","huggingface","invalid-device"],"backgroundTag":"invalid-enum-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}