MCPcopy Create free account
hub / github.com/eric-mitchell/direct-preference-optimization / pad_to_length

Function pad_to_length

utils.py:74–80  ·  view source on GitHub ↗
(tensor: torch.Tensor, length: int, pad_value: Union[int, float], dim: int = -1)

Source from the content-addressed store, hash-verified

72
73
74def pad_to_length(tensor: torch.Tensor, length: int, pad_value: Union[int, float], dim: int = -1) -> torch.Tensor:
75 if tensor.size(dim) >= length:
76 return tensor
77 else:
78 pad_size = list(tensor.shape)
79 pad_size[dim] = length - tensor.size(dim)
80 return torch.cat([tensor, pad_value * torch.ones(*pad_size, dtype=tensor.dtype, device=tensor.device)], dim=dim)
81
82
83def all_gather_if_needed(values: torch.Tensor, rank: int, world_size: int) -> torch.Tensor:

Callers 2

concatenated_inputsFunction · 0.90
get_batch_samplesMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected