4242def tensors_to_cpu_and_double (vars_ : list [torch .Tensor ]) -> list [torch .Tensor ]:
4343 """Convert a list of tensors to CPU and double precision.
4444
45+ This function ensures all tensors in the input list are moved to CPU and converted
46+ to double precision (float64) format. This is useful for operations requiring higher
47+ numerical precision.
48+
4549 Args:
46- vars_: List of PyTorch tensors to convert
50+ vars_: List of PyTorch tensors to convert. Tensors can be on any device.
4751
4852 Returns:
49- List of tensors converted to CPU and double precision
53+ List of tensors converted to CPU and double precision. The order of tensors
54+ in the output list matches the input list.
55+
56+ Example:
57+ >>> tensors = [torch.randn(3, 3).cuda(), torch.randn(2, 2)]
58+ >>> cpu_tensors = tensors_to_cpu_and_double(tensors)
59+ >>> all(t.is_cpu for t in cpu_tensors)
60+ True
61+ >>> all(t.dtype == torch.float64 for t in cpu_tensors)
62+ True
5063 """
5164 cpu_vars = []
5265 for v in vars_ :
@@ -60,12 +73,24 @@ def tensors_to_cuda(vars_: list[torch.Tensor],
6073 cuda_device : str ) -> list [torch .Tensor ]:
6174 """Convert a list of tensors to CUDA device.
6275
76+ This function moves all tensors in the input list to the specified CUDA device.
77+ Tensors already on CUDA are left unchanged. This is useful for GPU-accelerated
78+ computations.
79+
6380 Args:
64- vars_: List of PyTorch tensors to convert
65- cuda_device: CUDA device to move tensors to
81+ vars_: List of PyTorch tensors to convert. Tensors can be on any device.
82+ cuda_device: CUDA device identifier (e.g., "cuda:0", "cuda:1") to move
83+ tensors to.
6684
6785 Returns:
68- List of tensors moved to specified CUDA device
86+ List of tensors moved to the specified CUDA device. The order of tensors
87+ in the output list matches the input list.
88+
89+ Example:
90+ >>> tensors = [torch.randn(3, 3), torch.randn(2, 2)]
91+ >>> cuda_tensors = tensors_to_cuda(tensors, "cuda:0")
92+ >>> all(t.is_cuda for t in cuda_tensors)
93+ True
6994 """
7095 cpu_vars = []
7196 for v in vars_ :
@@ -87,19 +112,39 @@ def compute_jacobian(
87112 """Compute the Jacobian matrix for a given model and input.
88113
89114 This function computes the Jacobian matrix using PyTorch's autograd functionality.
90- It supports both CPU and CUDA computation, as well as single and double precision.
115+ The Jacobian represents the first-order partial derivatives of the model's output
116+ with respect to its input parameters. It supports both CPU and CUDA computation,
117+ as well as single and double precision.
91118
92119 Args:
93- model: PyTorch model to compute Jacobian for
94- input_vars: List of input tensors
95- mode: Computation mode, currently only "autograd" is supported
96- cuda_device: Device to use for CUDA computation
97- double_precision: If True, use double precision
98- convert_to_numpy: If True, convert output to numpy array
99- hybrid_solver: If True, concatenate multiple outputs along dimension 1
120+ model: PyTorch model to compute Jacobian for. The model should be callable
121+ with the input variables.
122+ input_vars: List of input tensors to compute the Jacobian with respect to.
123+ Each tensor should have requires_grad=True if gradients are needed.
124+ mode: Computation mode. Currently only "autograd" is supported, which uses
125+ PyTorch's automatic differentiation.
126+ cuda_device: CUDA device identifier to use for computation (e.g., "cuda:0").
127+ Ignored if double_precision is True.
128+ double_precision: If True, use double precision (float64) for computation.
129+ This moves all tensors to CPU.
130+ convert_to_numpy: If True, convert the output Jacobian to a numpy array.
131+ If False, returns a PyTorch tensor.
132+ hybrid_solver: If True, concatenate multiple model outputs along dimension 1
133+ before computing the Jacobian. Useful for models with multiple outputs.
100134
101135 Returns:
102- Jacobian matrix as either PyTorch tensor or numpy array
136+ Jacobian matrix as either a PyTorch tensor or numpy array. The shape is
137+ (batch_size, output_dim, input_dim), where:
138+ - batch_size is the size of the input batch
139+ - output_dim is the dimension of the model's output
140+ - input_dim is the total dimension of all input variables
141+
142+ Example:
143+ >>> model = torch.nn.Linear(10, 5)
144+ >>> x = torch.randn(3, 10, requires_grad=True)
145+ >>> jacobian = compute_jacobian(model, [x])
146+ >>> jacobian.shape
147+ (3, 5, 10)
103148 """
104149 if double_precision :
105150 model = model .to ("cpu" ).double ()
0 commit comments