gemini-code-assist[bot] commented on code in PR #19860:
URL: https://github.com/apache/tvm/pull/19860#discussion_r3449514729
##########
python/tvm/relax/frontend/tflite/tflite_frontend.py:
##########
@@ -6688,6 +6727,93 @@ def convert_embedding_lookup_sparse(self, op):
relax.op.greater(bucket_counts, relax.const(0.0, "float32")),
normalized, value_base
)
+ def convert_lsh_projection(self, op):
+ """Reject TFLite LSH_PROJECTION with a targeted diagnostic."""
+ from tflite.LSHProjectionOptions import LSHProjectionOptions
+ from tflite.LSHProjectionType import LSHProjectionType
+ from tflite.TensorType import TensorType
+
+ input_tensors = self.get_input_tensors(op)
+ output_tensors = self.get_output_tensors(op)
+ if len(input_tensors) not in (2, 3) or len(output_tensors) != 1:
+ raise tvm.error.OpNotImplemented(
+ "LSH_PROJECTION expects hash and input tensors, optional
weights, and one output"
+ )
+
+ hash_tensor, input_tensor = input_tensors[:2]
+ output_tensor = output_tensors[0]
+ hash_shape = to_int_list(self.get_tensor_shape(hash_tensor))
+ input_shape = to_int_list(self.get_tensor_shape(input_tensor))
+ output_shape = to_int_list(self.get_tensor_shape(output_tensor))
+ if hash_tensor.tensor.Type() != TensorType.FLOAT32:
+ raise tvm.error.OpNotImplemented("LSH_PROJECTION hash tensor must
be float32")
+ if len(hash_shape) != 2 or hash_shape[0] < 1 or hash_shape[1] > 32:
+ raise tvm.error.OpNotImplemented(
+ "LSH_PROJECTION hash tensor must be rank 2 with at most 32
bits"
+ )
+ if len(input_shape) < 1 or input_shape[0] < 1:
+ raise tvm.error.OpNotImplemented("LSH_PROJECTION input tensor must
be rank >= 1")
+ if len(input_tensors) == 3:
+ weight_tensor = input_tensors[2]
+ weight_shape = to_int_list(self.get_tensor_shape(weight_tensor))
+ if weight_tensor.tensor.Type() != TensorType.FLOAT32 or
weight_shape != [
+ input_shape[0]
+ ]:
+ raise tvm.error.OpNotImplemented(
+ "LSH_PROJECTION weights must be rank-1 float32 and match
input dimension 0"
+ )
Review Comment:

In TFLite, the weights tensor for `LSH_PROJECTION` is optional. If a model
does not provide weights, the third input index in the operator's inputs array
is `-1`, which causes `self.get_input_tensors(op)` to return `None` at index 2.
If `input_tensors[2]` is `None`, calling
`self.get_tensor_shape(weight_tensor)` will raise an `AttributeError` and
crash. We should check if `input_tensors[2]` is not `None` before attempting to
validate the weights.
```suggestion
if len(input_tensors) == 3 and input_tensors[2] is not None:
weight_tensor = input_tensors[2]
weight_shape = to_int_list(self.get_tensor_shape(weight_tensor))
if weight_tensor.tensor.Type() != TensorType.FLOAT32 or
weight_shape != [
input_shape[0]
]:
raise tvm.error.OpNotImplemented(
"LSH_PROJECTION weights must be rank-1 float32 and match
input dimension 0"
)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]