Merged
fix(multi_add_rms_norm_dynamic_quant): insert graph example TensorList at input zero #13852
zsq_888Add on 16 days ago
fix(multi_add_rms_norm_dynamic_quant): insert graph example TensorList at input zero #13852
Merged
Total: 1 files changed+54-1
| @@ -22,6 +22,8 @@ | |||
| 22 | #include <new> | 22 | #include <new> |
| 23 | #include <ctime> | 23 | #include <ctime> |
| 24 | #include <cstdint> | 24 | #include <cstdint> |
| 25 | +#include <cmath> | ||
| 26 | +#include <cstring> | ||
| 25 | #include <cstdlib> | 27 | #include <cstdlib> |
| 26 | #include "graph.h" | 28 | #include "graph.h" |
| 27 | #include "types.h" | 29 | #include "types.h" |
| @@ -87,7 +89,8 @@ int CreateOppInGraph(vector<Tensor>& input, vector<Operator>& inputs, vector<Ope | |||
| 87 | int idx = 0; | 89 | int idx = 0; |
| 88 | 90 | ||
| 89 | // x1: dynamic input tensor list -> multi-add | 91 | // x1: dynamic input tensor list -> multi-add |
| 90 | - op1.create_dynamic_input_x1(x1Num); | 92 | + // x1 is the first IR input; insert before the static input ports. |
| 93 | + op1.create_dynamic_input_byindex_x1(x1Num, 0); | ||
| 91 | for (int i = 0; i < x1Num; ++i) { | 94 | for (int i = 0; i < x1Num; ++i) { |
| 92 | auto d = MkHalf("x1_" + std::to_string(i), idx++, xShape, input); | 95 | auto d = MkHalf("x1_" + std::to_string(i), idx++, xShape, input); |
| 93 | graph.AddOp(d); | 96 | graph.AddOp(d); |
| @@ -132,6 +135,49 @@ int CreateOppInGraph(vector<Tensor>& input, vector<Operator>& inputs, vector<Ope | |||
| 132 | return SUCCESS; | 135 | return SUCCESS; |
| 133 | } | 136 | } |
| 134 | 137 | ||
| 138 | +// The example fills every input with 1.0. Check all six outputs against | ||
| 139 | +// this deterministic reference, including the TensorList contribution. | ||
| 140 | +static bool CheckExampleOutputs(const vector<ge::Tensor>& output) | ||
| 141 | +{ | ||
| 142 | + if (g_x1Num < 1 || g_x1Num > 5 || output.size() != 6) { | ||
| 143 | + return false; | ||
| 144 | + } | ||
| 145 | + const uint16_t sumBits[] = {0x4000, 0x4200, 0x4400, 0x4500, 0x4600}; | ||
| 146 | + const double sum = g_x1Num + 1.0; | ||
| 147 | + const double scale = sum / std::sqrt(sum * sum + 1e-6) / 127.0; | ||
| 148 | + for (size_t i = 0; i < output.size(); ++i) { | ||
| 149 | + const vector<int64_t> expectedShape = i < 4 ? vector<int64_t>{4, 8} : vector<int64_t>{4}; | ||
| 150 | + const DataType dtype = i < 2 ? DT_INT8 : (i < 4 ? DT_FLOAT16 : DT_FLOAT); | ||
| 151 | + const size_t count = i < 4 ? 32 : 4; | ||
| 152 | + const size_t width = i < 2 ? 1 : (i < 4 ? 2 : 4); | ||
| 153 | + if (output[i].GetTensorDesc().GetShape().GetDims() != expectedShape || | ||
| 154 | + output[i].GetTensorDesc().GetDataType() != dtype || output[i].GetSize() != count * width || | ||
| 155 | + output[i].GetData() == nullptr) { | ||
| 156 | + return false; | ||
| 157 | + } | ||
| 158 | + for (size_t j = 0; j < count; ++j) { | ||
| 159 | + const auto* data = output[i].GetData() + j * width; | ||
| 160 | + if (i < 2) { | ||
| 161 | + int8_t value; | ||
| 162 | + std::memcpy(&value, data, sizeof(value)); | ||
| 163 | + if (value != 127) | ||
| 164 | + return false; | ||
| 165 | + } else if (i < 4) { | ||
| 166 | + uint16_t value; | ||
| 167 | + std::memcpy(&value, data, sizeof(value)); | ||
| 168 | + if (value != (i == 2 ? sumBits[g_x1Num - 1] : 0x3c00)) | ||
| 169 | + return false; | ||
| 170 | + } else { | ||
| 171 | + float value; | ||
| 172 | + std::memcpy(&value, data, sizeof(value)); | ||
| 173 | + if (!std::isfinite(value) || std::abs(value - scale) > 1e-6) | ||
| 174 | + return false; | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + } | ||
| 178 | + return true; | ||
| 179 | +} | ||
| 180 | + | ||
| 135 | int main(int argc, char* argv[]) | 181 | int main(int argc, char* argv[]) |
| 136 | { | 182 | { |
| 137 | if (argc > 1) { | 183 | if (argc > 1) { |
| @@ -179,6 +225,13 @@ int main(int argc, char* argv[]) | |||
| 179 | return FAILED; | 225 | return FAILED; |
| 180 | } | 226 | } |
| 181 | printf("%s - INFO - [XIR]: Run graph success, output num = %zu\n", GetTime().c_str(), output.size()); | 227 | printf("%s - INFO - [XIR]: Run graph success, output num = %zu\n", GetTime().c_str(), output.size()); |
| 228 | + if (!CheckExampleOutputs(output)) { | ||
| 229 | + printf("Example output validation failed\n"); | ||
| 230 | + delete session; | ||
| 231 | + GEFinalize(); | ||
| 232 | + return FAILED; | ||
| 233 | + } | ||
| 234 | + printf("All six outputs match the deterministic reference\n"); | ||
| 182 | 235 | ||
| 183 | delete session; | 236 | delete session; |
| 184 | if (ge::GEFinalize() != SUCCESS) { | 237 | if (ge::GEFinalize() != SUCCESS) { |