Converts GetOnlineFeaturesResponse features into a dictionary of tensors or lists. - Numeric features (int, float, bool) -> torch.Tensor - Non-numeric features (e.g., strings) -> list[Any] Args: kind: Backend tensor type. Currently only "torch" is suppo
(
self,
kind: str = "torch",
default_value: Any = float("nan"),
)
| 109 | return pa.Table.from_pydict(result) |
| 110 | |
| 111 | def to_tensor( |
| 112 | self, |
| 113 | kind: str = "torch", |
| 114 | default_value: Any = float("nan"), |
| 115 | ) -> Dict[str, Union[TorchTensor, List[Any]]]: |
| 116 | """ |
| 117 | Converts GetOnlineFeaturesResponse features into a dictionary of tensors or lists. |
| 118 | |
| 119 | - Numeric features (int, float, bool) -> torch.Tensor |
| 120 | - Non-numeric features (e.g., strings) -> list[Any] |
| 121 | |
| 122 | Args: |
| 123 | kind: Backend tensor type. Currently only "torch" is supported. |
| 124 | default_value: Value to substitute for missing (None) entries. |
| 125 | |
| 126 | Returns: |
| 127 | Dict[str, Union[torch.Tensor, List[Any]]]: Mapping of feature names to tensors or lists. |
| 128 | """ |
| 129 | if kind != "torch": |
| 130 | raise ValueError( |
| 131 | f"Unsupported tensor kind: {kind}. Only 'torch' is supported currently." |
| 132 | ) |
| 133 | torch = get_torch() |
| 134 | feature_dict = self.to_dict(include_event_timestamps=False) |
| 135 | feature_keys = set(self.proto.metadata.feature_names.val) |
| 136 | tensor_dict: Dict[str, Union[TorchTensor, List[Any]]] = {} |
| 137 | for key in feature_keys: |
| 138 | raw_values = feature_dict[key] |
| 139 | values = [v if v is not None else default_value for v in raw_values] |
| 140 | first_valid = next((v for v in values if v is not None), None) |
| 141 | if isinstance(first_valid, (int, float, bool)): |
| 142 | try: |
| 143 | device = "cuda" if torch.cuda.is_available() else "cpu" |
| 144 | tensor_dict[key] = torch.tensor(values, device=device) |
| 145 | except Exception as e: |
| 146 | raise ValueError( |
| 147 | f"Failed to convert values for '{key}' to tensor: {e}" |
| 148 | ) |
| 149 | else: |
| 150 | tensor_dict[key] = ( |
| 151 | values # Return as-is for strings or unsupported types |
| 152 | ) |
| 153 | return tensor_dict |
| 154 | |
| 155 | |
| 156 | def _convert_uuids_for_arrow(result: Dict[str, List[Any]]) -> Dict[str, List[Any]]: |