|
| 1 | +# Copyright The FMS Model Optimizer Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Simple example using a toy model to demo how to trigger mx in fms-mo.""" |
| 15 | + |
| 16 | +# Third Party |
| 17 | +import numpy as np |
| 18 | +import torch |
| 19 | +import torch.nn.functional as F |
| 20 | + |
| 21 | + |
| 22 | +class ResidualMLP(torch.nn.Module): |
| 23 | + def __init__(self, hidden_size, device="cuda"): |
| 24 | + super(ResidualMLP, self).__init__() |
| 25 | + |
| 26 | + self.layernorm = torch.nn.LayerNorm(hidden_size, device=device) |
| 27 | + self.dense_4h = torch.nn.Linear(hidden_size, 4 * hidden_size, device=device) |
| 28 | + self.dense_h = torch.nn.Linear(4 * hidden_size, hidden_size, device=device) |
| 29 | + self.dummy = torch.nn.Linear(hidden_size, hidden_size, device=device) |
| 30 | + # add a dummy layer because by default we skip 1st/last, if there are only 2 layers, all will be skipped |
| 31 | + |
| 32 | + def forward(self, inputs): |
| 33 | + norm_outputs = self.layernorm(inputs) |
| 34 | + |
| 35 | + # MLP |
| 36 | + proj_outputs = self.dense_4h(norm_outputs) |
| 37 | + proj_outputs = F.gelu(proj_outputs) |
| 38 | + mlp_outputs = self.dense_h(proj_outputs) |
| 39 | + mlp_outputs = self.dummy(mlp_outputs) |
| 40 | + |
| 41 | + # Residual Connection |
| 42 | + outputs = inputs + mlp_outputs |
| 43 | + |
| 44 | + return outputs |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + # Third Party |
| 49 | + from tabulate import tabulate |
| 50 | + |
| 51 | + # Local |
| 52 | + from fms_mo import qconfig_init, qmodel_prep |
| 53 | + |
| 54 | + HIDDEN_DIM = 128 |
| 55 | + x = np.random.randn(16, HIDDEN_DIM) |
| 56 | + x = torch.tensor(x, dtype=torch.float32, device="cuda") |
| 57 | + results = { |
| 58 | + "dtype": [], |
| 59 | + "output[0, 0]": [], |
| 60 | + "output[0, 1]": [], |
| 61 | + "output[0, 2]": [], |
| 62 | + "||ref - out_dtype||_2": [], |
| 63 | + } |
| 64 | + |
| 65 | + # --- Test 0. Run MLP as is |
| 66 | + model = ResidualMLP(HIDDEN_DIM) |
| 67 | + with torch.no_grad(): |
| 68 | + out = model(x) |
| 69 | + results["dtype"].append("fp32") |
| 70 | + results["output[0, 0]"].append(out[0, 0]) |
| 71 | + results["output[0, 1]"].append(out[0, 1]) |
| 72 | + results["output[0, 2]"].append(out[0, 2]) |
| 73 | + results["||ref - out_dtype||_2"].append(0) |
| 74 | + print(model) |
| 75 | + |
| 76 | + # --- Test 1. fms-mo qmodel_prep, replace Linear with our QLinear |
| 77 | + qcfg = qconfig_init() |
| 78 | + qcfg["nbits_a"] = 8 |
| 79 | + qcfg["nbits_w"] = 8 |
| 80 | + qmodel_prep(model, x, qcfg) |
| 81 | + with torch.no_grad(): |
| 82 | + out_dtype = model(x) |
| 83 | + results["dtype"].append("fmsmo_int8") |
| 84 | + results["output[0, 0]"].append(out_dtype[0, 0]) |
| 85 | + results["output[0, 1]"].append(out_dtype[0, 1]) |
| 86 | + results["output[0, 2]"].append(out_dtype[0, 2]) |
| 87 | + results["||ref - out_dtype||_2"].append(torch.norm(out - out_dtype).item()) |
| 88 | + print(model) |
| 89 | + |
| 90 | + qcfg["nbits_a"] = 4 |
| 91 | + qcfg["nbits_w"] = 4 |
| 92 | + model = ResidualMLP(HIDDEN_DIM) |
| 93 | + qmodel_prep(model, x, qcfg) |
| 94 | + with torch.no_grad(): |
| 95 | + out_dtype = model(x) |
| 96 | + results["dtype"].append("fmsmo_int4") |
| 97 | + results["output[0, 0]"].append(out_dtype[0, 0]) |
| 98 | + results["output[0, 1]"].append(out_dtype[0, 1]) |
| 99 | + results["output[0, 2]"].append(out_dtype[0, 2]) |
| 100 | + results["||ref - out_dtype||_2"].append(torch.norm(out - out_dtype).item()) |
| 101 | + print(model) |
| 102 | + |
| 103 | + # --- Test 2. now change mapping to MX |
| 104 | + # NOTE simply use qa_mode or qw_mode to trigger the use of mx, e.g. use "mx_" prefixed mode, |
| 105 | + # qcfg["mapping"] and other qcfg["mx_specs"] content will be updated automatically |
| 106 | + |
| 107 | + for dtype_to_test in ["int8", "int4", "fp8_e4m3", "fp8_e5m2", "fp4_e2m1"]: |
| 108 | + qcfg["qw_mode"] = f"mx_{dtype_to_test}" |
| 109 | + qcfg["qa_mode"] = f"mx_{dtype_to_test}" |
| 110 | + model = ResidualMLP(HIDDEN_DIM) # fresh model |
| 111 | + qmodel_prep(model, x, qcfg) |
| 112 | + with torch.no_grad(): |
| 113 | + out_dtype = model(x) |
| 114 | + results["dtype"].append(f"mx{dtype_to_test}") |
| 115 | + results["output[0, 0]"].append(out_dtype[0, 0]) |
| 116 | + results["output[0, 1]"].append(out_dtype[0, 1]) |
| 117 | + results["output[0, 2]"].append(out_dtype[0, 2]) |
| 118 | + results["||ref - out_dtype||_2"].append(torch.norm(out - out_dtype).item()) |
| 119 | + print(model) |
| 120 | + |
| 121 | + print(tabulate(results, headers="keys", tablefmt="pipe", floatfmt=".4f")) |
| 122 | + |
| 123 | + print("DONE!") |
0 commit comments