error_helpers.cc 9.42 KB
Newer Older
1
2
3
/*
 * Helper functions for nicer runtime error messages.
 */
4
5
#include "error_helpers.h"

6
#include <tvm/ffi/c_api.h>
7
8
#include <tvm/ffi/error.h>
#include <tvm/ffi/function.h>
9
10
#include <tvm/ffi/reflection/registry.h>
#include <tvm/runtime/data_type.h>
11
#include <tvm/runtime/device_api.h>
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <sstream>
#include <string>

namespace tvm {
namespace tl {

// Return non-zero so that tvm_call_packed sites treat it as failure and return
// -1.
static int DTypeMismatch(const tvm::ffi::String &kernel_name,
                         const tvm::ffi::String &buffer_name,
                         int64_t actual_code, int64_t actual_bits,
                         int64_t actual_lanes, int64_t expect_code,
                         int64_t expect_bits, int64_t expect_lanes) {
  tvm::runtime::DataType actual(static_cast<int>(actual_code),
                                static_cast<int>(actual_bits),
                                static_cast<int>(actual_lanes));
  tvm::runtime::DataType expect(static_cast<int>(expect_code),
                                static_cast<int>(expect_bits),
                                static_cast<int>(expect_lanes));
  std::ostringstream os;
33
34
35
  os << "kernel " << std::string(kernel_name) << " input "
     << std::string(buffer_name) << " dtype expected " << expect << ", but got "
     << actual;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
  return -1;
}

// Variant without names, to avoid passing extra raw strings through packed
// args.
static int DTypeMismatchNoNames(int64_t actual_code, int64_t actual_bits,
                                int64_t actual_lanes, int64_t expect_code,
                                int64_t expect_bits, int64_t expect_lanes) {
  tvm::runtime::DataType actual(static_cast<int>(actual_code),
                                static_cast<int>(actual_bits),
                                static_cast<int>(actual_lanes));
  tvm::runtime::DataType expect(static_cast<int>(expect_code),
                                static_cast<int>(expect_bits),
                                static_cast<int>(expect_lanes));
  std::ostringstream os;
  os << "dtype mismatch: expected " << expect << ", but got " << actual;
  TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
  return -1;
}

57
// Register packed versions, following the design in runtime.cc
58
59
TVM_FFI_STATIC_INIT_BLOCK() {
  namespace refl = tvm::ffi::reflection;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

  // Packed: __tvm_error_dtype_mismatch(kernel_name, buffer_name,
  //                                    actual_code, actual_bits, actual_lanes,
  //                                    expect_code, expect_bits, expect_lanes)
  refl::GlobalDef().def_packed(
      tl::tvm_error_dtype_mismatch,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 8) << "Expected 8 args: kernel, buffer, "
                                    "actual_code, actual_bits, actual_lanes, "
                                 << "expect_code, expect_bits, expect_lanes";

        auto kernel_name = args[0].cast<tvm::ffi::String>();
        auto buffer_name = args[1].cast<tvm::ffi::String>();
        int64_t actual_code = args[2].cast<int64_t>();
        int64_t actual_bits = args[3].cast<int64_t>();
        int64_t actual_lanes = args[4].cast<int64_t>();
        int64_t expect_code = args[5].cast<int64_t>();
        int64_t expect_bits = args[6].cast<int64_t>();
        int64_t expect_lanes = args[7].cast<int64_t>();

        // Reuse the helper to format the message
        (void)DTypeMismatch(kernel_name, buffer_name, actual_code, actual_bits,
                            actual_lanes, expect_code, expect_bits,
                            expect_lanes);
        // Provide a return value for completeness, then signal the error
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, expect:int64, got:int64
  refl::GlobalDef().def_packed(
      tl::tvm_error_ndim_mismatch,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 4)
            << "__tvm_error_ndim_mismatch(kernel, buffer, expect, got)";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        int64_t expect = args[2].cast<int64_t>();
        int64_t got = args[3].cast<int64_t>();
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << " ndim expected " << expect << ", but got "
           << got;
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, expect:int64, got:int64
  refl::GlobalDef().def_packed(
      tl::tvm_error_byte_offset_mismatch,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 4)
            << "__tvm_error_byte_offset_mismatch(kernel, buffer, expect, got)";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        int64_t expect = args[2].cast<int64_t>();
        int64_t got = args[3].cast<int64_t>();
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << " byte_offset expected " << expect
           << ", but got " << got;
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, expect:int64, got:int64
  refl::GlobalDef().def_packed(
      tl::tvm_error_device_type_mismatch,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 4)
            << "__tvm_error_device_type_mismatch(kernel, buffer, expect, got)";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        int64_t expect = args[2].cast<int64_t>();
        int64_t got = args[3].cast<int64_t>();
        const char *expect_str =
            tvm::runtime::DLDeviceType2Str(static_cast<int>(expect));
        const char *got_str =
            tvm::runtime::DLDeviceType2Str(static_cast<int>(got));
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << " device_type expected " << expect_str
           << ", but got " << got_str;
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, field:String
  refl::GlobalDef().def_packed(
      tl::tvm_error_null_ptr,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 3)
            << "__tvm_error_null_ptr(kernel, buffer, field)";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        auto field = args[2].cast<tvm::ffi::String>();
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << ' ' << std::string(field)
           << " expected non-NULL, but got NULL";
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, field:String, expect:int64, got:int64
  refl::GlobalDef().def_packed(
      tl::tvm_error_expect_eq,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 5)
            << "__tvm_error_expect_eq(kernel, buffer, field, expect, got)";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        auto field = args[2].cast<tvm::ffi::String>();
        int64_t expect = args[3].cast<int64_t>();
        int64_t got = args[4].cast<int64_t>();
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << ' ' << std::string(field) << " expected "
           << expect << ", but got " << got;
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // kernel, buffer, field:String [, reason:String]
  refl::GlobalDef().def_packed(
      tl::tvm_error_constraint_violation,
      [](tvm::ffi::PackedArgs args, tvm::ffi::Any *ret) {
        ICHECK(args.size() == 3 || args.size() == 4)
            << "__tvm_error_constraint_violation(kernel, buffer, field[, "
               "reason])";
        auto kernel = args[0].cast<tvm::ffi::String>();
        auto buffer = args[1].cast<tvm::ffi::String>();
        auto field = args[2].cast<tvm::ffi::String>();
        std::string reason;
        if (args.size() == 4) {
          reason = args[3].cast<tvm::ffi::String>();
        }
        std::ostringstream os;
        os << "kernel " << std::string(kernel) << " input "
           << std::string(buffer) << ' ' << std::string(field)
           << " constraint not satisfied";
        if (!reason.empty()) {
          os << ": " << reason;
        }
        TVMFFIErrorSetRaisedFromCStr("RuntimeError", os.str().c_str());
        *ret = -1;
        throw ::tvm::ffi::EnvErrorAlreadySet();
      });

  // Legacy typed registrations for backward compatibility
215
216
217
218
219
  refl::GlobalDef().def("tilelang_error_dtype_mismatch",
                        &tvm::tl::DTypeMismatch);
  refl::GlobalDef().def("tilelang_error_dtype_mismatch2",
                        &tvm::tl::DTypeMismatchNoNames);
}
220
221
222

} // namespace tl
} // namespace tvm