apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.4k stars 634 forks source link

[ExecuTorch] Cannot Use Dynamic Index to Select #2189

Open YifanShenSZ opened 6 months ago

YifanShenSZ commented 6 months ago

This toy model fails in ExecuTorch

        class M(torch.nn.Module):
            def forward(self, float_arr, int_arr):
                dynamic_index = int_arr[1]
                float_arr[dynamic_index] = 12.95
                return float_arr

due to

E               torch._dynamo.exc.UserError: Consider annotating your code using torch._constrain_as_*(). Could not guard on data-dependent expression u0 (unhinted: u0).  (Size-like symbols: none)
E               
E               Potential framework code culprit (scroll up for full backtrace):
E                 File "<eval_with_key>.223", line 8, in forward
E                   l_float_arr_[dynamic_index] = 12.95;  setitem = l_float_arr_;  dynamic_index = None
E               
E               For more information, run with TORCH_LOGS="dynamic"
E               For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u0"
E               If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
E               For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing
E               
E               For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
E               For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#constrain-as-size-example

But some other dynamic indexing models are fine, e.g.

        class IndexPutModel(torch.nn.Module):
            def forward(self, x, position, val):
                y = x.clone()
                y[:, position] = val
                return y
YifanShenSZ commented 6 months ago

Similarly, dynamic slice would also fail

        class DynamicSlicer(torch.nn.Module):
            def forward(self, x, context_length):
                return x[context_length:, :, :]

due to

torch._dynamo.exc.Unsupported: Dynamic slicing on data-dependent value is not supported