https://github.com/c-bata/onnx-remove-random-normal
Automatically replace ONNX's RandomNormal node with Constant node.
https://github.com/c-bata/onnx-remove-random-normal
Last synced: 10 months ago
JSON representation
Automatically replace ONNX's RandomNormal node with Constant node.
- Host: GitHub
- URL: https://github.com/c-bata/onnx-remove-random-normal
- Owner: c-bata
- License: mit
- Created: 2021-12-11T16:05:19.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-12-11T16:05:48.000Z (over 4 years ago)
- Last Synced: 2025-04-04T16:28:19.937Z (about 1 year ago)
- Language: Python
- Size: 20.5 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# onnx-remove-random-normal
This is a script to replace RandomNormal node with Constant node.
## Example
Imagine that we have something ONNX model like the following:
```python
import torch
class SomethingModel(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor):
y = torch.tensor([-1, 2], dtype=torch.float32) # Constant node
z = torch.randn(2, dtype=torch.float32) # RandomNormal node
return x + y + z
def export_onnx(path: str) -> None:
x = torch.tensor([10, 20], dtype=torch.float32)
model = SomethingModel()
model.eval()
torch.onnx.export(
model, x, path,
verbose=True,
export_params=True,
do_constant_folding=False,
opset_version=11,
input_names=['x'],
output_names=['output'],
)
```
`torch.randn(...)` will be converted into `RandomNormal` node in ONNX.
However, we can replace it with something constants in some cases.
```
$ python onnx_remove_random_normal.py --help
usage: onnx_remove_random_normal.py [-h] [--seed SEED] source output
Replace ONNX RandomNormal node with Constant Node
positional arguments:
source source onnx file
output output onnx file
optional arguments:
-h, --help show this help message and exit
--seed SEED random seed
$ python onnx_remove_random_normal.py before.onnx after.onnx
```
| Before | After |
| --- | --- |
|  |  |