Skip to content
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
3fcc2c1
[COLLAGE] Add more customization to support more targets
krishnaraj36 Nov 21, 2022
8a8b622
Fix the lint errors
krishnaraj36 Nov 21, 2022
8c509c5
Fix the lint error whitespace
krishnaraj36 Nov 21, 2022
2c54172
Fix the lint error tabs
krishnaraj36 Nov 22, 2022
c707bd9
Fix the lint error tabs
krishnaraj36 Nov 22, 2022
1b4114b
Fix the lint error tabs
krishnaraj36 Nov 22, 2022
f8b6609
move the clml collage test case to test_clml
krishnaraj36 Nov 25, 2022
a61b5b0
Fix lint error whitespace
krishnaraj36 Nov 25, 2022
40bd214
Fix the import error
krishnaraj36 Nov 28, 2022
021e542
Fix the envirnoment var and import
krishnaraj36 Nov 29, 2022
4742574
Add comments
krishnaraj36 Nov 29, 2022
859845a
Add clml preprocess module in cost estimator
krishnaraj36 Dec 1, 2022
ecd6901
Fix whitespace lint error
krishnaraj36 Dec 1, 2022
30acd26
Fix whitespace lint error
krishnaraj36 Dec 1, 2022
7b3fdf3
Fix whitespace lint error
krishnaraj36 Dec 1, 2022
cd0e178
Fix the comments and removed unwanted code
krishnaraj36 Dec 13, 2022
5cbd53d
Fix whitespace error
krishnaraj36 Dec 13, 2022
0414eae
Merge branch 'main' into collage_update
krishnaraj36 Jan 5, 2023
9ef2cad
Removed Todo comments
krishnaraj36 Jan 5, 2023
a935ae9
Removed TODO comments
krishnaraj36 Jan 5, 2023
056885e
Updated naming convension
krishnaraj36 Jan 6, 2023
5804b08
Fix typo error
krishnaraj36 Jan 6, 2023
85b25cb
Fixe the typo error
krishnaraj36 Jan 6, 2023
d5e10f4
Corrected typo error
krishnaraj36 Jan 9, 2023
5f2bd89
Corrected typo error
krishnaraj36 Jan 9, 2023
fb2181d
Removed unused and fix typo error
krishnaraj36 Jan 9, 2023
bd42a90
Removed redundent code and optimize the code
krishnaraj36 Jan 9, 2023
b5ca5c0
Fix the lint error
krishnaraj36 Jan 9, 2023
513366a
Fix whitespace lint error
krishnaraj36 Jan 9, 2023
af47069
Removed Prints in file
krishnaraj36 Jan 10, 2023
2e840f1
Fix lint error
krishnaraj36 Jan 10, 2023
f02d639
Fix lint error
krishnaraj36 Jan 10, 2023
a72b61e
Removed runner template in test script
krishnaraj36 Jan 10, 2023
6951d46
Fix the lint error
krishnaraj36 Jan 10, 2023
7367a71
Fix lint error
krishnaraj36 Jan 10, 2023
d2a7274
Fix lint error
krishnaraj36 Jan 10, 2023
9330d4b
Fix the lint error
krishnaraj36 Jan 10, 2023
4a3d5a2
Fix the lint error
krishnaraj36 Jan 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/tvm/relay/collage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
WARMUP_MIN_REPEAT_MS,
CostEstimator,
MockCostEstimator,
CustomCostEstimator,
)
8 changes: 8 additions & 0 deletions python/tvm/relay/collage/collage.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def __init__(self, target_costs, max_estimates=0):
self.__init_handle_by_constructor__(_ffi_api.MockCostEstimator, target_costs, max_estimates)


@register_object("relay.collage.CustomCostEstimator")
class CustomCostEstimator(Object):
"""CustomEstimator class"""

def __init__(self, py_fn_estimator="tvm.relay.collage.estimate_seconds_custom"):
self.__init_handle_by_constructor__(_ffi_api.CustomCostEstimator, py_fn_estimator)


def arg_for(arg_type, device):
"""Returns a test argument of Relay arg_type on device"""
assert isinstance(arg_type, tvm.ir.TensorType)
Expand Down
20 changes: 20 additions & 0 deletions python/tvm/relay/op/contrib/clml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from tvm._ffi import register_func
from tvm.relay import transform
from tvm.relay.build_module import bind_params_by_name
from tvm.relay import function as _function
from tvm.relay.expr_functor import ExprMutator
from tvm.relay.expr import Call, TupleGetItem

Expand Down Expand Up @@ -161,6 +162,25 @@ def alter_conv(attrs, inputs, tinfos, out_type):
return preprocessed_mod


def preprocess_for_clml(mod):
"""Preprocessing pass to alter the layouts for CLML compiler target"""

for _var in mod.get_global_vars():
if _var.name_hint == "main":
continue
fn = mod[_var.name_hint]
if "Compiler" in fn.attrs.keys() and fn.attrs["Compiler"] == "clml":
new_fn = fn.body
clml_mod = tvm.IRModule.from_expr(new_fn)
with tvm.transform.PassContext(opt_level=3):
clml_mod = preprocess_module(clml_mod)
new_body = clml_mod["main"].body
mod[_var.name_hint] = _function.Function(
fn.params, new_body, fn.ret_type, fn.type_params, fn.attrs
)
return mod


@register_pattern_table("clml")
def clml_pattern_table():
"""Get the CLML pattern table."""
Expand Down
2 changes: 1 addition & 1 deletion src/relay/collage/collage_partitioner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace {

TVM_REGISTER_PASS_CONFIG_OPTION("relay.collage.tvm_max_depth", Integer);
TVM_REGISTER_PASS_CONFIG_OPTION("relay.collage.byoc_max_depth", Integer);

TVM_REGISTER_PASS_CONFIG_OPTION("relay.collage.byoc_fusion_style", Array<String>);
/*!
* \brief Represents the overall expression after some number of non-overlapping candidate
* partitions have been applied.
Expand Down
60 changes: 60 additions & 0 deletions src/relay/collage/custom_cost_estimator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 src/relay/collage/custom_cost_estimator.cc
* \brief A custom CostEstimator to support alternative cost functions.
*/

#include "./custom_cost_estimator.h"

#include <tvm/relay/expr_functor.h>

namespace tvm {
namespace relay {
namespace collage {

TVM_REGISTER_OBJECT_TYPE(CustomCostEstimatorNode);

Cost CustomCostEstimatorNode::Estimate(const IRModule& mod, const Target& target) const {
static const runtime::PackedFunc* estimate_seconds = runtime::Registry::Get(py_fn_estimator_);
ICHECK(estimate_seconds);
const double value = (*estimate_seconds)(mod, target);
if (std::isinf(value)) {
return Cost::Invalid();
} else if (std::isnan(value)) {
return Cost::Unknown();
} else {
return Cost::Value(value);
}
}

CustomCostEstimator::CustomCostEstimator(String py_fn_estimator) {
auto node = make_object<CustomCostEstimatorNode>();
node->py_fn_estimator_ = std::move(py_fn_estimator);
data_ = std::move(node);
}

TVM_REGISTER_GLOBAL("relay.collage.CustomCostEstimator").set_body_typed([](String py_fn_estimator) {
return CustomCostEstimator(std::move(py_fn_estimator));
});

} // namespace collage
} // namespace relay
} // namespace tvm
67 changes: 67 additions & 0 deletions src/relay/collage/custom_cost_estimator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 src/relay/collage/custom_cost_estimator.cc
* \brief A custom CostEstimator to support target-specific cost functions.
*/

#ifndef TVM_RELAY_COLLAGE_CUSTOM_COST_ESTIMATOR_H_
#define TVM_RELAY_COLLAGE_CUSTOM_COST_ESTIMATOR_H_

#include <tvm/relay/function.h>

#include "./cost.h"
#include "./cost_estimator.h"

namespace tvm {
namespace relay {
namespace collage {

/*!
* \brief A cost estimator that uses a target-specific cost function.
*/
class CustomCostEstimatorNode : public CostEstimatorNode {
public:
Cost Estimate(const IRModule& mod, const Target& target) const override;

static constexpr const char* _type_key = "relay.collage.CustomCostEstimator";
TVM_DECLARE_FINAL_OBJECT_INFO(CustomCostEstimatorNode, CostEstimatorNode);

protected:
/*!
* \brief Python implemented cost function name.
*/
String py_fn_estimator_;

friend class CustomCostEstimator;
};

class CustomCostEstimator : public CostEstimator {
public:
explicit CustomCostEstimator(String py_fn_estimator);

TVM_DEFINE_OBJECT_REF_METHODS(CustomCostEstimator, CostEstimator, CustomCostEstimatorNode);
};

} // namespace collage
} // namespace relay
} // namespace tvm

#endif // TVM_RELAY_COLLAGE_CUSTOM_COST_ESTIMATOR_H_
38 changes: 34 additions & 4 deletions src/relay/collage/gather_partition_specs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ PartitionRule MakeTVMPartitionRule() {
}

/*!
* \brief Returns the fusion style for \p compiler.
*
* TODO(mbs): Defer to per-BYOC integration definition.
* \brief Returns the fusion style for default compiler.
*/
BYOCStyle BYOCFusionStyleForCompiler(const String& compiler) {
BYOCStyle DefaultBYOCFusionStyleForCompiler(const String& compiler) {
if (compiler == "cutlass" || compiler == "cublas" || compiler == "cudnn") {
return kNoFusionBYOCStyle;
} else if (compiler == "tensorrt") {
Expand All @@ -103,6 +101,38 @@ BYOCStyle BYOCFusionStyleForCompiler(const String& compiler) {
}
}

/*!
* \brief Returns the fusion style for given compiler.
*/
BYOCStyle BYOCFusionStyleForCompiler(const String& compiler) {
tvm::transform::PassContext ctxt = tvm::transform::PassContext::Current();
std::string config_key = "relay.collage.byoc_fusion_style";
Optional<Array<String>> byoc_configs = ctxt->GetConfig(config_key, Optional<Array<String>>());
BYOCStyle byoc_fusion_style = DefaultBYOCFusionStyleForCompiler(compiler);
if (!byoc_configs) {
LOG(INFO)<<"default fusion style";
return byoc_fusion_style;
}
for (auto config_ : byoc_configs.value()) {
std::vector<std::string> byoc_cfg = SplitString(config_, ".");
LOG(INFO) <<"byoc cfg: "<<byoc_cfg[0]<<" : "<<byoc_cfg[1];
if (byoc_cfg[0] == compiler) {
if (byoc_cfg[1] == "NoFusion") {
LOG(INFO)<<"NoFusion detected";
byoc_fusion_style = kNoFusionBYOCStyle;
} else if (byoc_cfg[1] == "TVMFusion") {
byoc_fusion_style = kTVMFusionBYOCStyle;
} else if (byoc_cfg[1] == "ArbitraryFusion") {
byoc_fusion_style = kArbitraryFusionBYOCStyle;
} else {
ICHECK(false) << "Invalid fusion name for compiler " << byoc_cfg[0] << " in pass context";
}
break;
}
}
return byoc_fusion_style;
}

/*!
* \brief Returns the primitive combiner rules which allow for any touching candidates
* to be fused provided they don't have kind \p kOpaque.
Expand Down
13 changes: 13 additions & 0 deletions src/relay/collage/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ bool MustBeLowered(const Expr& expr) {
return false;
}

std::vector<std::string> SplitString(std::string stmt, const char* del) {
std::vector<std::string> str_tokens;
int start=0;
int end = stmt.find(del, 0);
str_tokens.emplace_back(stmt.substr(start, end));
while(end != -1) {
stmt = stmt.substr(end+1, stmt.size());
end = stmt.find(del, 0);
str_tokens.emplace_back(stmt.substr(start, end));
}
return str_tokens;
}

} // namespace collage
} // namespace relay
} // namespace tvm
5 changes: 5 additions & 0 deletions src/relay/collage/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ bool IsSpecialOp(const OpNode* op_node);
*/
bool MustBeLowered(const Expr& expr);

/*!
* \brief Returns the list of split strings of given statement with delimiter.
*/
std::vector<std::string> SplitString(std::string stmt, const char* del);

} // namespace collage
} // namespace relay
} // namespace tvm
Expand Down
Loading