|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# SPDX-FileCopyrightText: Copyright (c) 1993-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | + |
| 19 | +import numpy as np |
| 20 | +import onnx |
| 21 | +import onnx_graphsurgeon as gs |
| 22 | + |
| 23 | + |
| 24 | +hidden_size = 8 |
| 25 | +num_heads = 2 |
| 26 | +head_dim = hidden_size // num_heads |
| 27 | +sequence_length = 4 |
| 28 | + |
| 29 | +tokens = gs.Variable("tokens", dtype=np.float32, shape=(1, sequence_length, hidden_size)) |
| 30 | +identity_out = gs.Variable( |
| 31 | + "identity_out", dtype=np.float32, shape=(1, sequence_length, hidden_size) |
| 32 | +) |
| 33 | +split_shape = gs.Constant( |
| 34 | + "split_shape", values=np.array([1, sequence_length, num_heads, head_dim], dtype=np.int64) |
| 35 | +) |
| 36 | +merged_shape = gs.Constant( |
| 37 | + "merged_shape", values=np.array([1, sequence_length, hidden_size], dtype=np.int64) |
| 38 | +) |
| 39 | +split_heads = gs.Variable( |
| 40 | + "split_heads", dtype=np.float32, shape=(1, sequence_length, num_heads, head_dim) |
| 41 | +) |
| 42 | +heads_first = gs.Variable( |
| 43 | + "heads_first", dtype=np.float32, shape=(1, num_heads, sequence_length, head_dim) |
| 44 | +) |
| 45 | +tokens_again = gs.Variable( |
| 46 | + "tokens_again", dtype=np.float32, shape=(1, sequence_length, num_heads, head_dim) |
| 47 | +) |
| 48 | +merged = gs.Variable("merged", dtype=np.float32, shape=(1, sequence_length, hidden_size)) |
| 49 | + |
| 50 | +weights = gs.Constant( |
| 51 | + "projection_weight", |
| 52 | + values=np.linspace(-0.5, 0.5, hidden_size * hidden_size, dtype=np.float32).reshape( |
| 53 | + hidden_size, hidden_size |
| 54 | + ), |
| 55 | +) |
| 56 | +bias = gs.Constant( |
| 57 | + "projection_bias", |
| 58 | + values=np.linspace(-0.1, 0.1, hidden_size, dtype=np.float32), |
| 59 | +) |
| 60 | +projected = gs.Variable("projected", dtype=np.float32, shape=(1, sequence_length, hidden_size)) |
| 61 | +output = gs.Variable("output", dtype=np.float32, shape=(1, sequence_length, hidden_size)) |
| 62 | + |
| 63 | +nodes = [ |
| 64 | + gs.Node("Identity", inputs=[tokens], outputs=[identity_out]), |
| 65 | + gs.Node("Reshape", inputs=[identity_out, split_shape], outputs=[split_heads]), |
| 66 | + gs.Node( |
| 67 | + "Transpose", |
| 68 | + attrs={"perm": [0, 2, 1, 3]}, |
| 69 | + inputs=[split_heads], |
| 70 | + outputs=[heads_first], |
| 71 | + ), |
| 72 | + gs.Node( |
| 73 | + "Transpose", |
| 74 | + attrs={"perm": [0, 2, 1, 3]}, |
| 75 | + inputs=[heads_first], |
| 76 | + outputs=[tokens_again], |
| 77 | + ), |
| 78 | + gs.Node("Reshape", inputs=[tokens_again, merged_shape], outputs=[merged]), |
| 79 | + gs.Node("MatMul", inputs=[merged, weights], outputs=[projected]), |
| 80 | + gs.Node("Add", inputs=[projected, bias], outputs=[output]), |
| 81 | +] |
| 82 | + |
| 83 | +graph = gs.Graph(nodes=nodes, inputs=[tokens], outputs=[output], opset=18) |
| 84 | +model = gs.export_onnx(graph.cleanup().toposort()) |
| 85 | +onnx.checker.check_model(model) |
| 86 | +onnx.save(model, "model.onnx") |
0 commit comments