Create an appropriate input for the model.
(
model: nn.Module,
input_shape: str,
dtype: torch.dtype,
device: str = "cuda",
)
| 228 | |
| 229 | |
| 230 | def make_model_input( |
| 231 | model: nn.Module, |
| 232 | input_shape: str, |
| 233 | dtype: torch.dtype, |
| 234 | device: str = "cuda", |
| 235 | ) -> Union[torch.Tensor, Dict[str, torch.Tensor]]: |
| 236 | """Create an appropriate input for the model.""" |
| 237 | input_type = infer_input_type(model) |
| 238 | |
| 239 | if input_type == "token_ids": |
| 240 | # Language model: expects integer input_ids |
| 241 | dims = [int(d.strip()) for d in input_shape.split(",")] |
| 242 | torch.manual_seed(42) |
| 243 | input_ids = torch.randint(0, 32000, dims, device=device, dtype=torch.long) |
| 244 | |
| 245 | # Check if model accepts input_ids keyword |
| 246 | sig = inspect.signature(model.forward) |
| 247 | if "input_ids" in sig.parameters: |
| 248 | return {"input_ids": input_ids} |
| 249 | return input_ids |
| 250 | else: |
| 251 | return generate_sample_input(input_shape, dtype, device) |
| 252 | |
| 253 | |
| 254 | # --------------------------------------------------------------------------- |
no test coverage detected