yongwww commented on code in PR #14460:
URL: https://github.com/apache/tvm/pull/14460#discussion_r1174776353


##########
python/tvm/relax/frontend/stablehlo/stablehlo_translator.py:
##########
@@ -0,0 +1,431 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# pylint: disable=invalid-name, inconsistent-return-statements, 
unidiomatic-typecheck
+# pylint: disable=import-outside-toplevel, unused-argument, no-member
+"""StableHLO frontend of Relax."""
+from typing import Callable, Dict, List, Tuple, Union, Any
+
+import tvm
+from tvm import relax, tir
+
+
+class StableHLOImporter:
+    """An importer from StableHLO to Relax."""
+
+    from jaxlib import mlir
+    from jaxlib.mlir.dialects import stablehlo
+
+    def __init__(self) -> None:
+        from jaxlib import mlir
+
+        self._nodes: Dict[Union[str, mlir.ir.Operation], relax.Expr] = {}
+        self.block_builder: relax.BlockBuilder = None
+        self.create_convert_map()
+
+    @staticmethod
+    def _convert_data_type(input_type):
+        """converts the data type from mlir to tvm."""
+        from jaxlib import mlir
+
+        if mlir.ir.ShapedType.isinstance(input_type):
+            input_type = mlir.ir.ShapedType(input_type).element_type
+
+        input_type = str(input_type)
+        if input_type == "f16":
+            return "float16"
+        elif input_type in ["f32", "F32Type"]:
+            return "float32"
+        elif input_type in ["f64", "F64Type"]:
+            return "float64"
+        elif input_type == "i1":
+            return "bool"
+        elif input_type == "i8":
+            return "int8"
+        elif input_type == "i16":
+            return "int16"
+        elif input_type == "i32":
+            return "int32"
+        elif input_type == "i64":
+            return "int64"
+        elif input_type == "ui8":
+            return "uint8"
+        elif input_type == "ui16":
+            return "uint16"
+        elif input_type == "ui32":
+            return "uint32"
+        elif input_type == "ui64":
+            return "uint64"
+        else:
+            raise NotImplementedError(f"input_type {input_type} is not handled 
yet")
+
+    def _attr2value(self, node) -> Union[Any, List[Any]]:
+        from jaxlib import mlir
+        import numpy as np

Review Comment:
   When we try to import relax, the frontend will be imported as part of relax, 
import will failed if jaxlib is not installed. Making the imports inside 
function could help avoid this inconvenience (similar the handling as other 
translators)



-- 
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]

Reply via email to