You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow 0 size dimensions (dimensions containing a 0 in the list of sizes, not a rank of 0 which is valid) #391
In Chromium CL review, @fdwr mentioned (Thanks Dwayne!)
0 size dimensions really should just be treated as nops. e.g. memcpy(dest, src, 0) is valid and does nothing, and adding two empty tensors just returns an empty tensor. There are legitimate cases within a graph where a tensor may be temporarily sliced down to emptiness and then reconcatenated later with other data (I've come across at least one ONNX model that does this).
From native ML API perspective, Dwayne also mentioned
Now, there are certain backends that are not prepared for empty dimensions (e.g. DML doesn't accept them and rejects the operator creation call), and those operators will need to be skipped inside the backend, but the front-end model builder imo should treat them validly and return 0 elements.
We may want to investigate more native ML APIs to understand what's the status of the support.
I am opening this issue to start tracking, e.g. adding TODO in the implementation. @fdwr, feel free to share more details. Thanks!
In ONNX Runtime, these cases are handled as nops, either directly by the EP backend (if it handles them gracefully) or by the lower-level code just before it reaches the backend API call (such as with DirectML which currently rejects 0's in the dimensions, where the EP skips operator creation while still leaving the overall graph connectivity intact).
XNNPack
Allows them.
// see Bin Miao's code below
SafeTensors
The SafeTensors file format (commonly used with Stable Diffusion models for custom weights) explicitly allows 0D scalars and 0-size tensors - "Empty tensors (tensors with 1 dimension being 0) are allowed" and "0-rank Tensors (tensors with shape []) are allowed, they are merely a scalar".
I'm unsure what XNNPack would do if you tried to add two empty tensors (needs research).
@miaobin would volunteer to help investigate XNNPACK's support. Thanks!
After I deleted and modified the errant validation statement in the ml_graph_builder.cc and graph_validation_utils.cc. I verified that XNNPack supports both 0D scalars and 0-size tensors through the following two test cases:
miaobin: Great - thanks for investigating and adding the test cases. It will be more interesting for the DirectML backend because the current API rejects zero size tensors, and even if we were to update the API to accept them (and add test cases for all 100+ operators...), the older version would still be on the operating system. So we'll have to do the same thing like was done in ONNX Runtime where the operator creation is bypassed for such operators (the node is left null as a placeholder, and it's not added to the graph later).
There are legitimate cases within a graph where a tensor may be temporarily sliced down to emptiness and then reconcatenated later with other data (I've come across at least one ONNX model that does this).
WebNN's slice requires "the size must not be 0". Would this prevent the ONNX model you mentioned from slicing a tensor down to emptiness?
There are legitimate cases within a graph where a tensor may be temporarily sliced down to emptiness and then reconcatenated later with other data (I've come across at least one ONNX model that does this).
WebNN's slice requires "the size must not be 0". Would this prevent the ONNX model you mentioned from slicing a tensor down to emptiness?
@huningxin: 🤔 It could, as TF and ONNX support 0 size slices (see below). Granted, it's unlikely a TF or ONNX model would typically contain a 0-slice window (ends - starts = 0), but it could occur indirectly as a result of a model generation process and manipulating some other variable:
Bin Miao showed that XNNPack's add supports empty tensors fine, and so if it fails on concat, then that's a bug in XNNPack. Shall I open a GitHub issue, or do you want to? It doesn't matter either way for the WebNN EP though, because you just skip passing that input tensor to concat, the same as the ORT DML EP. So if there were 3 inputs (a=[2,3], b=[2,0], c=[2,4]), then only pass the nonzero ones to the XNNPack call (inputs = [a, c]).
Evidently LLaMA is another model that can encounter legal 0 size tensors during concat.
Another case that we should consider is having webnn input operands that have 0 dimensions. This is not allowed today.
However, for TinyLama the first round of next token generation requires representing the past key value tensor as a tensor of dimension [1,4,0,64] the graph then takes the .shape() of that tensor and performs operations on it to determine the size of other tensors it creates via generateConstantOfShape.
However, for TinyLama the first round of next token generation requires representing the past key value tensor as a tensor of dimension [1,4,0,64] the graph then takes the .shape() of that tensor and performs operations on it to determine the size of other tensors it creates via generateConstantOfShape.
Would this mean the shape of key value tensors keeps changing for each round of inference? WebNN only supports static shape. This may cause re-compiling WebNN graph for each round? We met similar issue for Whisper model inference. The static key value cache seems to be useful: huggingface/transformers#27931
If I permit MLBuffer to exist with 0-dim, what does it mean for DML to execute a IDMLCommandRecorder::RecordDispatch using a DML_BUFFER_BINDING::SizeInBytes equal to 0 and can this binding be NULL or left unbound?
@fdwr, you mentioned an ONNX model which depends on this. Can you elaborate on what this is used for in the model?
@reillyeon It will take some history digging for full context (like which operators in the model hit the issue). Two affected operators I recall were concatenation and slice. ⌛
I know it hit a few more models, but my email search is just turning up RCNN models (like MaskRCNN) with operators {Cast, Xor, Unsqueeze, Concat, Scatter with empty indices which becomes identity}, plus this ORT CUDA CR microsoft/onnxruntime#2337 (but I didn't see the context of impacted models for CUDA EP).
If I permit MLBuffer to exist with 0-dim, what does it mean for DML to execute a IDMLCommandRecorder::RecordDispatch using a DML_BUFFER_BINDING::SizeInBytes equal to 0 and can this binding be NULL or left unbound?
@bbernhar Currently the DML API rejects empty tensors anyway, but I've been thinking of relaxing that (we see that it actually "just works" for a lot of operators when the validation is relaxed), and I think we'd still need the binding even for emptiness (so not unbound).
it actually "just works" for a lot of operators when the validation is relaxed
So if we create an "empty binding" by giving IDMLCommandRecorder::RecordDispatch a 4B dummy buffer with a DML_BUFFER_BINDING::SizeInBytes of zero, DML API will NOT reject?
So if we create an "empty binding" by giving IDMLCommandRecorder::RecordDispatch a 4B dummy buffer with a DML_BUFFER_BINDING::SizeInBytes of zero, DML API will NOT reject?
@bbernhar I don't know what would happen in that case, but it's a moot point currently anyway because you cannot create an operator with empty tensors anyway (so you wouldn't even get as far as RecordDispatch). I just don't want to back ourselves into an inoperable corner where we can't support this in the DML API later (empty tensors have also been an issue when DML is called from TensorFlow, PyTorch, and ORT). Note it probably requires 16-byte dummy (per DML_MINIMUM_BUFFER_TENSOR_ALIGNMENT = 16).
Currently, we can't specify MLBuffer using an operator (or "in the graph"), only as input/output to dispatch() which basically calls nothing but RecordDispatch . We need to ensure we're not relying on undefined DML behavior - perhaps others on DML team have thoughts on this "dummy buffer" approach? I believe DML_MINIMUM_BUFFER_TENSOR_ALIGNMENT is the offset alignment requirement for bound buffers - DML buffer size alignment is 4B per MSDN [1]. Currently, the WebNN runtime disallows MLBuffer to be bound to non-zero offsets so only the size requirement matters, I think.
changed the title [-]Allow 0 size dimensions[/-][+]Allow 0 size dimensions (dimensions containing a 0 in the list of sizes, not a rank of 0 which is valid)[/+]on Aug 20, 2024
@bbernhar : Confirmed. Passing < 16 bytes is okay for DML_BUFFER_TENSOR_DESC::TotalTensorSizeInBytes, but it must be >= 4, or else you get: "The TotalTensorSizeInBytes of '...' for tensor '...' does not meet the minimum size required for this tensor, which is %llu bytes...".
The year is 2026, and the Chromium DirectML backend (which didn't support 0's in dimensions) no longer matters, as it's been removed from Chromium. Do all known backends support 0's in shapes now? CoreML remains uncertain (and alas, I don't have a machine to test that).
Can you confirm DML has been fully depreciated, even in WinML?
The ORT DML EP still exists and has logic to skip such operators (such that it never reaches the DML API). It was the Chromium DML backend that didn't.
Following up with CoreML results from my work in RustNN that I've tested on my small local device lab: M4/macOS 27, iPhone XS Max/iOS 18.7.10, Watch SE2/watchOS 11.6.2, and Series 10/watchOS 27. Empty-tensor behavior is operation- and backing-dependent: ordinary empty buffers fail, but retaining the logical zero extent with non-null backing makes empty/nonempty concat produce the correct result on all four devices. (There's a separate thing I'm tracking down that I ran into where Empty ReLU and our shape→slice→concat case still fail, but that doesn't invalidate this proposal.)
The results I got from working through the peculiarities (especially on watchOS which is touchy about operation order) suggests implementations need per-operation empty-tensor handling, rather than treating every empty input as a no-op. if its useful, I can contribute reduced tests for empty concat, shape(empty), and empty outputs as the spec changes land. My high-level exercise was getting SmolLM and TinyStories compiling, lowering, and running on Apple Watch -- I'm trying to get practical end-to-end results :)
Activity
fdwr commentedon May 23, 2023
References
All the major Python ML APIs handle them robustly, and they are not considered degenerate:
NumPy
TensorFlow
PyTorch
ONNX / ONNX Runtime
In ONNX Runtime, these cases are handled as nops, either directly by the EP backend (if it handles them gracefully) or by the lower-level code just before it reaches the backend API call (such as with DirectML which currently rejects 0's in the dimensions, where the EP skips operator creation while still leaving the overall graph connectivity intact).
XNNPack
Allows them.
// see Bin Miao's code belowSafeTensors
The SafeTensors file format (commonly used with Stable Diffusion models for custom weights) explicitly allows 0D scalars and 0-size tensors - "Empty tensors (tensors with 1 dimension being 0) are allowed" and "0-rank Tensors (tensors with shape []) are allowed, they are merely a scalar".
CoreML / MPS / BNNS
? Not evident from documentation:
DirectML
Disallows 0 for DML_BUFFER_TENSOR_DESC::Sizes. The backend must skip the operation.
huningxin commentedon Jun 30, 2023
@miaobin would volunteer to help investigate XNNPACK's support. Thanks!
miaobin commentedon Jul 1, 2023
After I deleted and modified the errant validation statement in the ml_graph_builder.cc and graph_validation_utils.cc. I verified that XNNPack supports both 0D scalars and 0-size tensors through the following two test cases:
Test for 0D scalars:
Test for 0-size tensors:
Both the tests have passed.
fdwr commentedon Jul 1, 2023
miaobin: Great - thanks for investigating and adding the test cases. It will be more interesting for the DirectML backend because the current API rejects zero size tensors, and even if we were to update the API to accept them (and add test cases for all 100+ operators...), the older version would still be on the operating system. So we'll have to do the same thing like was done in ONNX Runtime where the operator creation is bypassed for such operators (the node is left null as a placeholder, and it's not added to the graph later).
fdwr commentedon Sep 14, 2023
Evidently LLaMA is another model that can encounter legal 0 size tensors during
concat.huningxin commentedon Nov 27, 2023
@fdwr
WebNN's slice requires "the size must not be 0". Would this prevent the ONNX model you mentioned from slicing a tensor down to emptiness?
fdwr commentedon Nov 28, 2023
@huningxin: 🤔 It could, as TF and ONNX support 0 size slices (see below). Granted, it's unlikely a TF or ONNX model would typically contain a 0-slice window (ends - starts = 0), but it could occur indirectly as a result of a model generation process and manipulating some other variable:
TF
ONNX
huningxin commentedon Nov 29, 2023
According to my test, XNNPACK
concat(xnn_define_concatenate2/3/4) andsplit(xnn_define_even_split2/3/4) operators would report invalid parameter error (xnn_status_invalid_parameter) when the input has 0 size dimension.fdwr commentedon Nov 29, 2023
Bin Miao showed that XNNPack's
addsupports empty tensors fine, and so if it fails onconcat, then that's a bug in XNNPack. Shall I open a GitHub issue, or do you want to? It doesn't matter either way for the WebNN EP though, because you just skip passing that input tensor to concat, the same as the ORT DML EP. So if there were 3 inputs (a=[2,3], b=[2,0], c=[2,4]), then only pass the nonzero ones to the XNNPack call (inputs = [a, c]).huningxin commentedon Nov 30, 2023
Opened: google/XNNPACK#5807
If frameworks can handle that, it would help simplify WebNN implementation.
sushraja-msft commentedon Mar 18, 2024
Another case that we should consider is having webnn input operands that have 0 dimensions. This is not allowed today.
However, for TinyLama the first round of next token generation requires representing the past key value tensor as a tensor of dimension [1,4,0,64] the graph then takes the .shape() of that tensor and performs operations on it to determine the size of other tensors it creates via generateConstantOfShape.
fdwr commentedon Mar 18, 2024
And it sounds like from @guschmue today that this affects yolov9 too.
huningxin commentedon Mar 19, 2024
@sushraja-msft
Would this mean the shape of key value tensors keeps changing for each round of inference? WebNN only supports static shape. This may cause re-compiling WebNN graph for each round? We met similar issue for Whisper model inference. The static key value cache seems to be useful: huggingface/transformers#27931
3 remaining items
bbernhar commentedon Aug 19, 2024
@fdwr
If I permit
MLBufferto exist with 0-dim, what does it mean for DML to execute aIDMLCommandRecorder::RecordDispatchusing aDML_BUFFER_BINDING::SizeInBytesequal to 0 and can this binding be NULL or left unbound?fdwr commentedon Aug 19, 2024
I know it hit a few more models, but my email search is just turning up RCNN models (like MaskRCNN) with operators {Cast, Xor, Unsqueeze, Concat, Scatter with empty indices which becomes identity}, plus this ORT CUDA CR microsoft/onnxruntime#2337 (but I didn't see the context of impacted models for CUDA EP).
@bbernhar Currently the DML API rejects empty tensors anyway, but I've been thinking of relaxing that (we see that it actually "just works" for a lot of operators when the validation is relaxed), and I think we'd still need the binding even for emptiness (so not unbound).
bbernhar commentedon Aug 20, 2024
@fdwr
So if we create an "empty binding" by giving
IDMLCommandRecorder::RecordDispatcha 4B dummy buffer with aDML_BUFFER_BINDING::SizeInBytesof zero, DML API will NOT reject?fdwr commentedon Aug 20, 2024
@bbernhar I don't know what would happen in that case, but it's a moot point currently anyway because you cannot create an operator with empty tensors anyway (so you wouldn't even get as far as RecordDispatch). I just don't want to back ourselves into an inoperable corner where we can't support this in the DML API later (empty tensors have also been an issue when DML is called from TensorFlow, PyTorch, and ORT). Note it probably requires 16-byte dummy (per DML_MINIMUM_BUFFER_TENSOR_ALIGNMENT = 16).
bbernhar commentedon Aug 20, 2024
@fdwr
Currently, we can't specify
MLBufferusing an operator (or "in the graph"), only as input/output to dispatch() which basically calls nothing butRecordDispatch. We need to ensure we're not relying on undefined DML behavior - perhaps others on DML team have thoughts on this "dummy buffer" approach? I believeDML_MINIMUM_BUFFER_TENSOR_ALIGNMENTis the offset alignment requirement for bound buffers - DML buffer size alignment is 4B per MSDN [1]. Currently, the WebNN runtime disallowsMLBufferto be bound to non-zero offsets so only the size requirement matters, I think.[1] https://learn.microsoft.com/en-us/windows/ai/directml/dml-helper-functions#dmlcalcbuffertensorsize
[-]Allow 0 size dimensions[/-][+]Allow 0 size dimensions (dimensions containing a 0 in the list of sizes, not a rank of 0 which is valid)[/+]fdwr commentedon Aug 21, 2024
@bbernhar : Confirmed. Passing < 16 bytes is okay for
DML_BUFFER_TENSOR_DESC::TotalTensorSizeInBytes, but it must be >= 4, or else you get: "The TotalTensorSizeInBytes of '...' for tensor '...' does not meet the minimum size required for this tensor, which is %llu bytes...".fdwr commentedon May 7, 2026
The year is 2026, and the Chromium DirectML backend (which didn't support 0's in dimensions) no longer matters, as it's been removed from Chromium. Do all known backends support 0's in shapes now? CoreML remains uncertain (and alas, I don't have a machine to test that).
bbernhar commentedon May 8, 2026
Can you confirm DML has been fully depreciated, even in WinML?
fdwr commentedon May 8, 2026
The ORT DML EP still exists and has logic to skip such operators (such that it never reaches the DML API). It was the Chromium DML backend that didn't.
anssiko commentedon May 21, 2026
matthargett commentedon Sep 11, 2026
Following up with CoreML results from my work in RustNN that I've tested on my small local device lab: M4/macOS 27, iPhone XS Max/iOS 18.7.10, Watch SE2/watchOS 11.6.2, and Series 10/watchOS 27. Empty-tensor behavior is operation- and backing-dependent: ordinary empty buffers fail, but retaining the logical zero extent with non-null backing makes empty/nonempty concat produce the correct result on all four devices. (There's a separate thing I'm tracking down that I ran into where Empty ReLU and our shape→slice→concat case still fail, but that doesn't invalidate this proposal.)
The results I got from working through the peculiarities (especially on watchOS which is touchy about operation order) suggests implementations need per-operation empty-tensor handling, rather than treating every empty input as a no-op. if its useful, I can contribute reduced tests for empty concat, shape(empty), and empty outputs as the spec changes land. My high-level exercise was getting SmolLM and TinyStories compiling, lowering, and running on Apple Watch -- I'm trying to get practical end-to-end results :)
anssiko commentedon Sep 11, 2026
Running WebNN on your wrist is beyond cool, KUTGW!