utils
utils
¤
LOG_CLAMP_MIN = -708.3964185322641
module-attribute
¤
csafelog = ComplexSafeLog.apply
module-attribute
¤
safelog = SafeLog.apply
module-attribute
¤
ComplexSafeLog
¤
Bases: Function
A numerically safe natural logarithm autograd function for complex inputs.
In the backward pass, it replaces the NaNs and infinities arising in the gradient with zeros.
Source code in cirkit/backend/torch/utils.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
backward(ctx, grad_output)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
62 63 64 65 | |
forward(x)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
47 48 49 50 51 52 53 | |
setup_context(ctx, inputs, output)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
55 56 57 58 59 60 | |
SafeLog
¤
Bases: Function
A numerically safe natural logarithm autograd function.
In the backward pass, it replaces the NaNs and infinities arising in the gradient with zeros.
Source code in cirkit/backend/torch/utils.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
backward(ctx, grad_output)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
30 31 32 33 | |
forward(x)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
19 20 21 | |
setup_context(ctx, inputs, output)
staticmethod
¤
Source code in cirkit/backend/torch/utils.py
23 24 25 26 27 28 | |
flatten_dims(x, /, *, dims)
¤
Flatten the given dims in the input.
If the dims are not continuous, they will be permuted and flattened to the position of the first element in dims.
Intended to be used as a helper for some torch functions that can only work on one dim.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The tensor to be flattened. |
required |
dims
|
Sequence[int]
|
The dimensions to flatten along, expected to be sorted. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The flattened tensor. |
Source code in cirkit/backend/torch/utils.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
unflatten_dims(x, /, *, dims, shape)
¤
Unflatten the first dim in dims in the input to get a given shape.
This is the inverse transformation of flatten_dims, provided a correspondimg shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
The tensor to be unflattened. |
required |
dims
|
Sequence[int]
|
The dimensions to unflatten to, should be the same as flatten_dims. |
required |
shape
|
Sequence[int]
|
The shape to unflatten to, can be either the shape for dims, or the whole shape for the output. If the latter, the shape will not be checked for consistency outside dims. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor |
Tensor
|
The unflattened tensor. |
Source code in cirkit/backend/torch/utils.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |