This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Retiarii] support debug mode for easy debuggability (#3476)
- Loading branch information
1 parent
c71dadb
commit 200a108
Showing
5 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT license. | ||
|
||
import logging | ||
import os | ||
import random | ||
import string | ||
|
||
from .. import Sampler, codegen, utils | ||
from ..execution.base import BaseGraphData | ||
from .base import BaseStrategy | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
class ChooseFirstSampler(Sampler): | ||
def choice(self, candidates, mutator, model, index): | ||
return candidates[0] | ||
|
||
class _LocalDebugStrategy(BaseStrategy): | ||
""" | ||
This class is supposed to be used internally, for debugging trial mutation | ||
""" | ||
|
||
def run_one_model(self, model): | ||
graph_data = BaseGraphData(codegen.model_to_pytorch_script(model), model.evaluator) | ||
random_str = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(6)) | ||
file_name = f'_generated_model/{random_str}.py' | ||
os.makedirs(os.path.dirname(file_name), exist_ok=True) | ||
with open(file_name, 'w') as f: | ||
f.write(graph_data.model_script) | ||
model_cls = utils.import_(f'_generated_model.{random_str}._model') | ||
graph_data.evaluator._execute(model_cls) | ||
os.remove(file_name) | ||
|
||
def run(self, base_model, applied_mutators): | ||
_logger.info('local debug strategy has been started.') | ||
model = base_model | ||
_logger.debug('New model created. Applied mutators: %s', str(applied_mutators)) | ||
choose_first_sampler = ChooseFirstSampler() | ||
for mutator in applied_mutators: | ||
mutator.bind_sampler(choose_first_sampler) | ||
model = mutator.apply(model) | ||
# directly run models | ||
self.run_one_model(model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters