Skip to content

Support loading interpreter arguments from a file in stablehlo-translate - #2979

Open
hamzaqureshi5 wants to merge 5 commits into
openxla:mainfrom
hamzaqureshi5:interpreter-args-file
Open

Support loading interpreter arguments from a file in stablehlo-translate#2979
hamzaqureshi5 wants to merge 5 commits into
openxla:mainfrom
hamzaqureshi5:interpreter-args-file

Conversation

@hamzaqureshi5

@hamzaqureshi5 hamzaqureshi5 commented Jul 19, 2026

Copy link
Copy Markdown

Fixes #2912.

--args currently requires spelling out every input tensor inline, which
exceeds OS argument-length limits for realistically sized models. This PR
adds an IREE-style @ prefix for reading arguments from files, in two
commits:

1. --args=@file

The file contains the same array-attribute text as the inline form:

stablehlo-translate model.mlir --interpret --args=@inputs.mlir

2. --args=@a.npy,@b.npy

Comma-separated NumPy files, each providing one input tensor. The tensor
type is inferred from the file's header, so probe outputs
(--probe-output-dir) can be fed back in as interpreter inputs directly:

stablehlo-translate model.mlir --interpret --args=@probe1.npy,@probe2.npy

Type inference covers little-endian integer and f32/f64 dtypes. Types
without a native NumPy representation (bool, f16, bf16, complex) are
serialized by serializeTensor under substitute descrs, so they can't be
unambiguously inferred and are rejected with a clear error. Extending
these is left for a follow-up.

NumPy reader fixes

Wiring up inference exposed three latent bugs in NumPy.cpp, previously
unreachable because the parsed shape was never used. Note the last one
changes the descr that probe files are written with:

  • parseShapeHeader returned an ArrayRef to a local vector and
    pre-filled four zero dims; it now returns an owned, correctly-bounded
    vector, and the file's shape is validated against the expected type.
  • Header sizes above 127 bytes were sign-extended into negative values.
  • The i64/ui64 type dispatch was inverted relative to every other width,
    so signless i64 tensors serialized with a 'u8' descr and vice versa.
    Write and read stay consistent with each other, as before.

Testing

  • stablehlo/tests/interpret/api_input_arguments.mlir: @file positive
    and missing-file cases.
  • stablehlo/tests/interpret/api_input_arguments_npy.mlir: writes the
    arguments to .npy via interpreter.probe, then loads them back as
    --args inputs (round-trips the serializer/reader pair); plus
    missing-file and mixed-file error cases.
  • Manually verified against externally generated NumPy files (f32 2x2,
    '|i1' int8, '<i8' int64, and bool → rejected as unsupported).
  • ninja check-stablehlo-tests passes.

@google-cla

google-cla Bot commented Jul 19, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@hamzaqureshi5 hamzaqureshi5 changed the title Interpreter args file Support loading interpreter arguments from a file in stablehlo-translate Jul 19, 2026
@hamzaqureshi5

Copy link
Copy Markdown
Author

@GleasonK PR is ready for review

@abhigunj
abhigunj self-requested a review August 5, 2026 21:36
Comment thread stablehlo/tests/interpret/api_input_arguments.mlir Outdated
Comment thread stablehlo/tools/StablehloTranslateMain.cpp
@hamzaqureshi5
hamzaqureshi5 requested a review from abhigunj August 10, 2026 09:27
Inline --args puts entire input tensors on the command line, which
exceeds OS argument-length limits for realistically sized models.
Allow --args=@file, where the file contains the same array attribute
text as the inline form.

Fixes openxla#2912 (IREE-style @ prefix; .npy input support to follow).
Extend --args=@file to accept comma-separated .npy files, each
providing one input tensor with its type inferred from the NumPy
header. This closes the loop with interpreter.probe, whose serialized
outputs can now be fed back in as interpreter inputs.

Type inference covers little-endian integer and f32/f64 dtypes. Types
without a native NumPy representation (bool, f16, bf16, complex) are
serialized under substitute descrs and cannot be unambiguously
inferred, so they are rejected with a clear error.

Also fixes latent bugs in the NumPy reader that inference exposed
(previously unreachable because the parsed shape was never used):
- parseShapeHeader returned a dangling ArrayRef and prepended four
  zero dims; it now returns an owned, correctly-bounded vector, and
  the file's shape is validated against the expected type.
- Header sizes above 127 bytes were sign-extended into negative
  values.
- i64/ui64 type dispatch was inverted relative to every other width,
  so signless i64 tensors serialized with a 'u8' descr and vice versa.

Together with the previous commit, completes openxla#2912.
- Validate the .npy extension across all comma-separated args files
  instead of only the last one, so `--args=@a.npy,b.txt` reports
  "cannot mix .npy and non-.npy args files" rather than silently
  treating the whole string as a single file name. Multiple files
  remain supported only for .npy args.
- Restore api_input_arguments.mlir to its original contents and move
  the @file tests to a new api_input_arguments_from_file.mlir. File
  related errors now use their own usage hint, leaving the existing
  attribute parsing messages unchanged.
@abhigunj
abhigunj force-pushed the interpreter-args-file branch from c54d7c8 to 8545947 Compare August 17, 2026 22:16
@abhigunj

Copy link
Copy Markdown
Member

LGTM, lint check is failing, fix that. ready to merge.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Have a way to load inputs from a file in stablehlo-translate

2 participants