MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / compareTwoTensorV2

Function compareTwoTensorV2

examples/cpp/bert_fp8/bert_fp8_example.cc:38–115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36
37template<typename T1, typename T2>
38void compareTwoTensorV2(const T1* pred,
39 const T2* ref,
40 const int batch_size,
41 const int* sequence_lengths,
42 const int hidden_unit,
43 const int max_length,
44 const int print_size = 0,
45 const std::string filename = "",
46 const float& mean_rel_diff_threshold = -1.0f)
47{
48 std::vector<int> v_sequence_lengths(sequence_lengths, sequence_lengths + batch_size);
49 const int size = batch_size * max_length * hidden_unit;
50 T1* h_pred = new T1[size];
51 T2* h_ref = new T2[size];
52 check_cuda_error(cudaMemcpy(h_pred, pred, size * sizeof(T1), cudaMemcpyDeviceToHost));
53 check_cuda_error(cudaMemcpy(h_ref, ref, size * sizeof(T2), cudaMemcpyDeviceToHost));
54
55 FILE* fd = nullptr;
56 if (filename != "") {
57 fd = fopen(filename.c_str(), "w");
58 fprintf(fd, "| %10s | %10s | %10s | %10s | \n", "pred", "ref", "abs_diff", "rel_diff(%)");
59 }
60
61 if (print_size > 0) {
62 FT_LOG_INFO(" id | pred | ref |abs diff | rel diff (%) |");
63 }
64 float mean_abs_diff = 0.0f;
65 float mean_rel_diff = 0.0f;
66 int count = 0;
67 for (int i = 0; i < batch_size; i++) {
68 for (int j = 0; j < sequence_lengths[i] * hidden_unit; j++) {
69 const int idx = i * max_length * hidden_unit + j;
70 if (idx < print_size) {
71 FT_LOG_INFO("%4d | % 6.4f | % 6.4f | % 6.4f | % 7.4f |\r",
72 i,
73 (float)h_pred[idx],
74 (float)h_ref[idx],
75 abs((float)h_pred[idx] - (float)h_ref[idx]),
76 abs((float)h_pred[idx] - (float)h_ref[idx]) / (abs((float)h_ref[idx]) + 1e-6f) * 100.f);
77 }
78
79 if ((float)h_pred[idx] == 0) {
80 continue;
81 }
82 count += 1;
83 mean_abs_diff += abs((float)h_pred[idx] - (float)h_ref[idx]);
84 mean_rel_diff += abs((float)h_pred[idx] - (float)h_ref[idx]) / (abs((float)h_ref[idx]) + 1e-6f) * 100.f;
85
86 if (fd != nullptr) {
87 fprintf(fd,
88 "| %10.5f | %10.5f | %10.5f | %11.5f |\n",
89 (float)h_pred[idx],
90 (float)h_ref[idx],
91 abs((float)h_pred[idx] - (float)h_ref[idx]),
92 abs((float)h_pred[idx] - (float)h_ref[idx]) / (abs((float)h_ref[idx]) + 1e-6f) * 100.f);
93 }
94 }
95 }

Callers 1

bertExampleFunction · 0.85

Calls 1

fmtstrFunction · 0.85

Tested by

no test coverage detected