Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX][MetaSchedule] JSON dump FloatImm at least one decimal #14118

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/meta_schedule/database/database_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
#include <cmath>
#include <iomanip>
#include <sstream>
#include <vector>
Expand All @@ -40,7 +41,12 @@ void JSONDumps(ObjectRef json_obj, std::ostringstream& os) {
os << int_imm->value;
}
} else if (const auto* float_imm = json_obj.as<FloatImmNode>()) {
os << std::setprecision(20) << float_imm->value;
double int_part;
if (std::modf(float_imm->value, &int_part) == 0.0) {
os << std::fixed << std::setprecision(1) << float_imm->value;
} else {
os << std::setprecision(20) << float_imm->value;
}
} else if (const auto* str = json_obj.as<runtime::StringObj>()) {
os << '"' << support::StrEscape(str->data, str->size) << '"';
} else if (const auto* array = json_obj.as<runtime::ArrayNode>()) {
Expand Down
37 changes: 35 additions & 2 deletions tests/python/unittest/test_tir_schedule_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,40 @@ def test_trace_as_json_1():
]


def test_trace_as_json_floatimm():
var = tir.Var("v", "int32")
trace1 = Trace(
insts=[
Instruction(
kind=InstructionKind.get("SampleCategorical"),
inputs=[],
attrs=[[tvm.tir.IntImm("int32", 3)], [tvm.tir.FloatImm("float32", 1.0)]],
outputs=[var],
)
],
decisions={},
)
json1 = trace1.as_json()
assert json1 == [[["SampleCategorical", [], [[3], [1.0]], ["v0"]]], []]

trace2 = Trace(
insts=[
Instruction(
kind=InstructionKind.get("SampleCategorical"),
inputs=[],
attrs=[
[tvm.tir.IntImm("int32", 3), tvm.tir.IntImm("int32", 4)],
[tvm.tir.FloatImm("float32", 0.5), tvm.tir.FloatImm("float32", 0.5)],
],
outputs=[var],
)
],
decisions={},
)
json2 = trace2.as_json()
assert json2 == [[["SampleCategorical", [], [[3, 4], [0.5, 0.5]], ["v0"]]], []]


def test_trace_simplified_1():
trace = _make_trace_3(BlockRV(), BlockRV(), add_postproc=True)
assert str(trace) == "\n".join(
Expand Down Expand Up @@ -367,5 +401,4 @@ def test_apply_annotation_from_json():


if __name__ == "__main__":
test_trace_simplified_2()
# tvm.testing.main()
tvm.testing.main()