zxybazh commented on code in PR #14624: URL: https://github.com/apache/tvm/pull/14624#discussion_r1175577652
########## src/relax/transform/few_shot_tuning.cc: ########## @@ -0,0 +1,169 @@ +/* + * 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 <tvm/relax/transform.h> + +#include "../../meta_schedule/utils.h" + +namespace tvm { +namespace relax { +namespace transform { + +tir::PrimFunc FewShotTunePrimFunc(const tir::PrimFunc& prim_func, const Target& target, + int64_t valid_count, Optional<meta_schedule::Runner> runner) { + // fetch a local builder + static const auto* f_get_local_builder = + runtime::Registry::Get("meta_schedule.builder.get_local_builder"); + ICHECK(f_get_local_builder) + << "ValueError: Cannot find the packed function \"meta_schedule.builder.get_local_builder\""; + meta_schedule::Builder builder = (*f_get_local_builder)(); + // create an IRModule + IRModule mod = IRModule(Map<GlobalVar, BaseFunc>({{GlobalVar("main"), prim_func}})); + // fetch the number of physical cores + static const auto* f_cpu_count = runtime::Registry::Get("meta_schedule.cpu_count"); + ICHECK(f_cpu_count) << "ValueError: Cannot find the packed function \"meta_schedule._cpu_count\""; + int num_threads = (*f_cpu_count)(false); + // store the results + Array<IRModule> results; + std::vector<double> costs; + // create a TuneContext + meta_schedule::TuneContext task = meta_schedule::TuneContext( + /*mod=*/mod, + /*target=*/target, + /*space_generator=*/ + meta_schedule::SpaceGenerator::PostOrderApply(/*f_block_filter=*/nullptr, + /*sch_rules=*/NullOpt, + /*postprocs=*/NullOpt, + /*mutator_probs=*/NullOpt), + /*search_strategy=*/meta_schedule::SearchStrategy::ReplayTrace(/*max_fail_count=*/100), + /*task_name=*/NullOpt, + /*num_threads=*/num_threads, // use all available local threads + /*rand_state=*/-1, // -1 means use random seed + /*logger=*/nullptr); + task->Initialize(); + task->search_strategy.value()->PreTuning( + /*max_trials=*/valid_count, /*num_trials_per_iter=*/valid_count, + /*design_spaces=*/task->space_generator.value()->GenerateDesignSpace(mod), + /*database=*/NullOpt, + /*cost_model=*/NullOpt); + while (valid_count > 0) { + Optional<Array<meta_schedule::MeasureCandidate>> candidates = + task->search_strategy.value()->GenerateMeasureCandidates(); + if (!candidates.defined()) break; + Array<meta_schedule::BuilderInput> builder_inputs; + for (const meta_schedule::MeasureCandidate& candidate : candidates.value()) { + builder_inputs.push_back(meta_schedule::BuilderInput( + /*mod=*/candidate->sch->mod(), + /*target=*/target)); + } + Array<meta_schedule::BuilderResult> builder_results = builder->Build(builder_inputs); + ICHECK_EQ(builder_results.size(), candidates.value().size()); + int idx = 0; + for (const meta_schedule::BuilderResult& builder_result : builder_results) { + if (!builder_result->error_msg.defined()) { + results.push_back(candidates.value()[idx]->sch->mod()); + valid_count--; + } + idx++; + } + if (runner.defined()) { + Array<meta_schedule::RunnerInput> runner_inputs; + int idx = 0; + for (const meta_schedule::BuilderResult& builder_result : builder_results) { + if (!builder_result->error_msg.defined()) { + runner_inputs.push_back(meta_schedule::RunnerInput( + /*artifact_path=*/builder_result->artifact_path.value(), + /*device_type=*/target->kind->name, + /*args_info=*/candidates.value()[idx]->args_info)); + } + idx++; + } + Array<meta_schedule::RunnerFuture> runner_futures = runner.value()->Run(runner_inputs); + for (const meta_schedule::RunnerFuture& runner_future : runner_futures) { + meta_schedule::RunnerResult runner_result = runner_future->Result(); + if (runner_result->error_msg.defined()) { + costs.push_back(1e10); + } else { + double sum = 0; + for (const FloatImm& cost : runner_result->run_secs.value()) { + sum += cost->value; + } + costs.push_back(sum / runner_result->run_secs.value().size()); + } + } + ICHECK_EQ(costs.size(), results.size()); + } + } + if (results.size() == 0) { + LOG(WARNING) << "No valid schedule found"; + return prim_func; + } + int best_idx = 0; + if (runner.defined()) { + for (int i = 1; i < costs.size(); ++i) { + if (costs[i] < costs[best_idx]) { + best_idx = i; + } + } + } else { + best_idx = results.size() - 1; Review Comment: Good point, if `valid_count` is `1`, I think there's no gain / benefit to generate more candidate without benchmarking. We can fix the option to default to 1 when benchmarking is not enabled. -- 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]
