Skip to content

Operator.save_scales crashes with TypeError: Object of type float32 is not JSON serializable #41

Open

Description

@SID-6921

Description

operators/common/Operator.py''s save_scales casts min/max to native Python float before building the JSON-serializable dict, but not scale:

scale = max(abs(low), abs(high)) / 127.0
ranges_dict[key] = dict(scale=scale, min=float(low), max=float(high), offset=offset)

low/high come from ndarray.min()/ndarray.max() in save_inputs_and_ref_outputs (key_to_range[inp_tensor.name] = (inp_data.min(), inp_data.max())), so they are NumPy scalars, not Python floats. abs()/max()// on NumPy scalars return a NumPy scalar too, so scale stays a numpy.float32/float64. json.dumps cannot serialize that:

TypeError: Object of type float32 is not JSON serializable

Repro

This is 100% reproducible by just running the op reconstruction test suite as documented (python operators/test.py) — it crashes on the very first operator (Neg), confirmed locally.

Fix

Wrap scale in float(...) the same way min/max already are.

I have a one-line fix ready to submit as a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions