|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2025 The HuggingFace Inc. team. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +from dataclasses import dataclass |
| 17 | +from typing import Any |
| 18 | + |
| 19 | +import torch |
| 20 | + |
| 21 | +from lerobot.configs.types import PolicyFeature |
| 22 | +from lerobot.processor.pipeline import EnvTransition, TransitionKey |
| 23 | +from lerobot.utils.utils import get_safe_torch_device |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class DeviceProcessor: |
| 28 | + """Processes transitions by moving tensors to the specified device. |
| 29 | +
|
| 30 | + This processor ensures that all tensors in the transition are moved to the |
| 31 | + specified device (CPU or GPU) before they are returned. |
| 32 | + """ |
| 33 | + |
| 34 | + device: torch.device = "cpu" |
| 35 | + |
| 36 | + def __post_init__(self): |
| 37 | + self.device = get_safe_torch_device(self.device) |
| 38 | + self.non_blocking = "cuda" in str(self.device) |
| 39 | + |
| 40 | + def __call__(self, transition: EnvTransition) -> EnvTransition: |
| 41 | + # Create a copy of the transition |
| 42 | + new_transition = transition.copy() |
| 43 | + |
| 44 | + # Process observation tensors |
| 45 | + observation = transition.get(TransitionKey.OBSERVATION) |
| 46 | + if observation is not None: |
| 47 | + new_observation = { |
| 48 | + k: v.to(self.device, non_blocking=self.non_blocking) if isinstance(v, torch.Tensor) else v |
| 49 | + for k, v in observation.items() |
| 50 | + } |
| 51 | + new_transition[TransitionKey.OBSERVATION] = new_observation |
| 52 | + |
| 53 | + # Process action tensor |
| 54 | + action = transition.get(TransitionKey.ACTION) |
| 55 | + if action is not None and isinstance(action, torch.Tensor): |
| 56 | + new_transition[TransitionKey.ACTION] = action.to(self.device, non_blocking=self.non_blocking) |
| 57 | + |
| 58 | + # Process reward tensor |
| 59 | + reward = transition.get(TransitionKey.REWARD) |
| 60 | + if reward is not None and isinstance(reward, torch.Tensor): |
| 61 | + new_transition[TransitionKey.REWARD] = reward.to(self.device, non_blocking=self.non_blocking) |
| 62 | + |
| 63 | + # Process done tensor |
| 64 | + done = transition.get(TransitionKey.DONE) |
| 65 | + if done is not None and isinstance(done, torch.Tensor): |
| 66 | + new_transition[TransitionKey.DONE] = done.to(self.device, non_blocking=self.non_blocking) |
| 67 | + |
| 68 | + # Process truncated tensor |
| 69 | + truncated = transition.get(TransitionKey.TRUNCATED) |
| 70 | + if truncated is not None and isinstance(truncated, torch.Tensor): |
| 71 | + new_transition[TransitionKey.TRUNCATED] = truncated.to( |
| 72 | + self.device, non_blocking=self.non_blocking |
| 73 | + ) |
| 74 | + |
| 75 | + return new_transition |
| 76 | + |
| 77 | + def get_config(self) -> dict[str, Any]: |
| 78 | + """Return configuration for serialization.""" |
| 79 | + return {"device": self.device} |
| 80 | + |
| 81 | + def feature_contract(self, features: dict[str, PolicyFeature]) -> dict[str, PolicyFeature]: |
| 82 | + return features |
0 commit comments