areusch commented on a change in pull request #9088:
URL: https://github.com/apache/tvm/pull/9088#discussion_r716959073



##########
File path: src/relay/backend/name_transforms.cc
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.
+ */
+
+#include "name_transforms.h"
+
+#include <tvm/runtime/registry.h>
+
+#include <cctype>
+#include <string>
+
+namespace tvm {
+namespace relay {
+namespace backend {
+
+std::string ToCFunctionStyle(const std::string& original_name) {
+  ICHECK(!original_name.empty());
+
+  int tvm_prefix_length = 3;
+  std::string function_name("TVM");
+
+  bool new_block = true;
+  for (const char& symbol : original_name.substr(tvm_prefix_length)) {

Review comment:
       should we assert the initial prefix is "TVM"?

##########
File path: tests/python/relay/test_name_transforms.py
##########
@@ -0,0 +1,96 @@
+# 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.
+
+from tvm import TVMError
+from tvm.relay.backend.name_transforms import (
+    to_c_function_style,
+    to_c_variable_style,
+    prefix_name,
+    prefix_generated_name,
+    sanitise_name,
+)
+import pytest
+
+
+def test_to_c_function_style():
+    assert to_c_function_style("TVM_Woof") == "TVMWoof"

Review comment:
       can we add a test where the prefix is not TVM just to demonstrate?

##########
File path: tests/python/relay/test_name_transforms.py
##########
@@ -0,0 +1,96 @@
+# 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.
+
+from tvm import TVMError
+from tvm.relay.backend.name_transforms import (
+    to_c_function_style,
+    to_c_variable_style,
+    prefix_name,
+    prefix_generated_name,
+    sanitise_name,
+)
+import pytest
+
+
+def test_to_c_function_style():
+    assert to_c_function_style("TVM_Woof") == "TVMWoof"
+    assert to_c_function_style("TVM_woof") == "TVMWoof"
+    assert to_c_function_style("TVM_woof_woof") == "TVMWoofWoof"
+    assert to_c_function_style("TVMGen_woof_woof") == "TVMGenWoofWoof"
+
+    with pytest.raises(TVMError):
+        to_c_function_style("")
+
+
+def test_to_c_variable_style():
+    assert to_c_variable_style("TVM_Woof") == "tvm_woof"
+    assert to_c_variable_style("TVM_woof") == "tvm_woof"
+    assert to_c_variable_style("TVM_woof_Woof") == "tvm_woof_woof"
+
+    with pytest.raises(TVMError):

Review comment:
       shall we assert on the content of the error?

##########
File path: src/relay/backend/name_transforms.h
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file relay/backend/name_transforms.h
+ * \brief Transformations which are applied on names to generate appropriately 
named compiler
+ * artifacts
+ *
+ * Example:
+ * ToCFunctionStyle(PrefixName(CombineNames({"Device", "target", "Invoke"})))
+ * // TVMDeviceTargetInvoke
+ *
+ * ToCFunctionStyle(PrefixGeneratedName(CombineNames({"model", "Run"})))
+ * // TVMGenModelRun
+ *
+ * ToCVariableStyle(PrefixName(CombineNames({"Device", "target", "t"})))
+ * // tvm_device_target_t
+ *
+ * ToCVariableStyle(PrefixGeneratedName(CombineNames({"model", "Devices"})))
+ * // tvmgen_model_devices
+ *
+ */
+
+#include <tvm/runtime/container/array.h>
+#include <tvm/runtime/container/string.h>
+#include <tvm/runtime/logging.h>
+
+#include <algorithm>
+#include <iostream>
+#include <string>
+
+#ifndef TVM_RELAY_BACKEND_NAME_TRANSFORMS_H_
+#define TVM_RELAY_BACKEND_NAME_TRANSFORMS_H_
+
+namespace tvm {
+namespace relay {
+namespace backend {
+
+/*!
+ * \brief Transform a name to the C variable style assuming it is
+ * appropriately constructed using the prefixing functions
+ * \param original_name Original name
+ * \return Transformed function in the C function style
+ */
+std::string ToCFunctionStyle(const std::string& original_name);

Review comment:
       another option is to call this ToCamelCase and ToSnakeCase. thoughts on 
that ?

##########
File path: python/tvm/relay/backend/name_transforms.py
##########
@@ -0,0 +1,86 @@
+# 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.
+"""
+Name transformation functions for use in code generation
+"""
+
+from typing import List, Union
+
+from . import _backend
+
+
+def to_c_function_style(original_name: str):
+    """Transform a name to the C function style assuming it is
+    appropriately constructed using the prefixing functions
+
+    Parameters
+    ----------
+    original_name : str
+        Original name to transform
+    """
+    return _backend.ToCFunctionStyle(original_name)
+
+
+def to_c_variable_style(original_name: str):
+    """Transform a name to the C variable style assuming it is
+    appropriately constructed using the prefixing functions
+
+    Parameters
+    ----------
+    original_name : str
+        Original name to transform
+    """
+    return _backend.ToCVariableStyle(original_name)
+
+
+def prefix_name(names: Union[List[str], str]):
+    """Apply TVM-specific prefix to a function name
+
+    Parameters
+    ----------
+    names : Union[List[str], str]
+        List of names to combine to form a combined name or the name itself
+    """
+    if isinstance(names, str):
+        names = [names]
+
+    return _backend.PrefixName(names)
+
+
+def prefix_generated_name(names: Union[List[str], str]):
+    """Apply generated TVM-specific prefix to a function name
+
+    Parameters
+    ----------
+    names : Union[List[str], str]
+        List of names to combine to form a combined name or the name itself
+    """
+    if isinstance(names, str):
+        names = [names]
+
+    return _backend.PrefixGeneratedName(names)
+
+
+def sanitise_name(original_name: str):

Review comment:
       not that i realllly care which one we pick (then again does saying this 
amount to renouncing my american citizenship), but it would be great if we 
could use just one "ize" in the codebase. since "ized" is quite prevalent 
already and "ised" shows up in just a few comments, can i ask you to change to 
sanitize_name? 😬 




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