jroesch commented on code in PR #11199:
URL: https://github.com/apache/tvm/pull/11199#discussion_r863737070


##########
python/tvm/relay/transform/flexible_shape.py:
##########
@@ -0,0 +1,273 @@
+# 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, dangerous-default-value
+"""Relay functions for wrapping a module with flexible shape dispatch."""
+from tvm import relay
+
+
+def override_shape(tensor_type, dim, value):
+    """Change a value in a tensor shape."""
+    new_dims = list(tensor_type.shape)
+    new_dims[dim] = value
+    return relay.TensorType(new_dims, tensor_type.dtype)
+
+
+def specialize_body(mod, function, dim, value, input_indices=[0], 
affects_output=True):
+    """Create a subgraph to handle specific input shapes"""
+    # Iterate through specified inputs and construct specialized shapes for 
each.
+    new_params = list(function.params)
+    data_binding = {}
+    dyn_data_array = []
+    for inp in input_indices:
+        data = function.params[inp]
+        flex_ty = override_shape(data.type_annotation, dim, value)
+        dyn_data = relay.Var(data.name_hint, type_annotation=flex_ty)
+        new_params[inp] = dyn_data
+        data_binding[data] = dyn_data
+        dyn_data_array.append(dyn_data)
+
+    # Create a new function body for the modified shapes.
+    new_body = relay.expr.bind(function.body, data_binding)
+    # Only change the output shape if the input shape affects it.
+    if affects_output:
+        new_ret_ty = override_shape(function.ret_type, dim, value)
+    else:
+        new_ret_ty = function.ret_type
+    gvar = relay.GlobalVar("main_" + str(value))
+    # Add the new function to the main IRModule.
+    mod[gvar] = relay.Function(
+        new_params, new_body, new_ret_ty, function.type_params, function.attrs
+    )
+    return gvar, [d.type_annotation for d in dyn_data_array]
+
+
+def flexible_dispatch(
+    mod, dim=0, buckets=[1], auto_pad=False, pad_value=0, input_indices=[0], 
affects_output=True

Review Comment:
   Same comment as above, probably worth specifying the modes in the doc string 
if possible to enumerate. 



##########
python/tvm/relay/transform/flexible_shape.py:
##########
@@ -0,0 +1,273 @@
+# 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, dangerous-default-value
+"""Relay functions for wrapping a module with flexible shape dispatch."""
+from tvm import relay
+
+
+def override_shape(tensor_type, dim, value):
+    """Change a value in a tensor shape."""
+    new_dims = list(tensor_type.shape)
+    new_dims[dim] = value
+    return relay.TensorType(new_dims, tensor_type.dtype)
+
+
+def specialize_body(mod, function, dim, value, input_indices=[0], 
affects_output=True):
+    """Create a subgraph to handle specific input shapes"""
+    # Iterate through specified inputs and construct specialized shapes for 
each.
+    new_params = list(function.params)
+    data_binding = {}
+    dyn_data_array = []
+    for inp in input_indices:
+        data = function.params[inp]
+        flex_ty = override_shape(data.type_annotation, dim, value)
+        dyn_data = relay.Var(data.name_hint, type_annotation=flex_ty)
+        new_params[inp] = dyn_data
+        data_binding[data] = dyn_data
+        dyn_data_array.append(dyn_data)
+
+    # Create a new function body for the modified shapes.
+    new_body = relay.expr.bind(function.body, data_binding)
+    # Only change the output shape if the input shape affects it.
+    if affects_output:
+        new_ret_ty = override_shape(function.ret_type, dim, value)
+    else:
+        new_ret_ty = function.ret_type
+    gvar = relay.GlobalVar("main_" + str(value))
+    # Add the new function to the main IRModule.
+    mod[gvar] = relay.Function(
+        new_params, new_body, new_ret_ty, function.type_params, function.attrs
+    )
+    return gvar, [d.type_annotation for d in dyn_data_array]
+
+
+def flexible_dispatch(
+    mod, dim=0, buckets=[1], auto_pad=False, pad_value=0, input_indices=[0], 
affects_output=True

Review Comment:
   Same comment as above about defaults, probably worth specifying the modes in 
the doc string if possible to enumerate. 



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