|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Module for collect data of regalloc priority prediction.""" |
| 16 | + |
1 | 17 | import gin
|
2 | 18 | import tensorflow as tf
|
3 | 19 |
|
|
6 | 22 | import os
|
7 | 23 | import tempfile
|
8 | 24 | from typing import Dict, Optional, Tuple
|
9 |
| -from absl import logging |
10 | 25 |
|
11 |
| -from google.protobuf import struct_pb2 |
| 26 | +# This is https://github.com/google/pytype/issues/764 |
| 27 | +from google.protobuf import struct_pb2 # pytype: disable=pyi-error |
12 | 28 | from compiler_opt.rl import compilation_runner
|
13 | 29 |
|
14 | 30 |
|
15 | 31 | @gin.configurable(module='runners')
|
16 | 32 | class RegAllocPriorityRunner(compilation_runner.CompilationRunner):
|
17 |
| - def _compile_fn( |
18 |
| - self, file_paths: Tuple[str, ...], tf_policy_path: str, reward_only: bool, |
19 |
| - cancellation_manager: Optional[ |
20 |
| - compilation_runner.WorkerCancellationManager] |
21 |
| - ) -> Dict[str, Tuple[tf.train.SequenceExample, float]]: |
| 33 | + """Class for collecting data for regalloc-priority-prediction.""" |
| 34 | + |
| 35 | + def _compile_fn( |
| 36 | + self, file_paths: Tuple[str, ...], tf_policy_path: str, reward_only: bool, |
| 37 | + cancellation_manager: Optional[ |
| 38 | + compilation_runner.WorkerCancellationManager] |
| 39 | + ) -> Dict[str, Tuple[tf.train.SequenceExample, float]]: |
22 | 40 |
|
23 |
| - file_paths = file_paths[0].replace('.bc', '') |
24 |
| - working_dir = tempfile.mkdtemp() |
| 41 | + file_paths = file_paths[0].replace('.bc', '') |
| 42 | + working_dir = tempfile.mkdtemp() |
25 | 43 |
|
26 |
| - log_path = os.path.join(working_dir, 'log') |
27 |
| - output_native_path = os.path.join(working_dir, 'native') |
| 44 | + log_path = os.path.join(working_dir, 'log') |
| 45 | + output_native_path = os.path.join(working_dir, 'native') |
28 | 46 |
|
29 |
| - result = {} |
30 |
| - try: |
31 |
| - command_line = [] |
32 |
| - if self._launcher_path: |
33 |
| - command_line.append(self._launcher_path) |
34 |
| - command_line.extend([self._clang_path] + [ |
35 |
| - '-c', file_paths, '-O3', |
36 |
| - '-mllvm', '-regalloc-priority-training-log=' + log_path, |
37 |
| - '-mllvm', '-regalloc-enable-priority-advisor=development', |
38 |
| - '-o', output_native_path |
39 |
| - ]) |
| 47 | + result = {} |
| 48 | + try: |
| 49 | + command_line = [] |
| 50 | + if self._launcher_path: |
| 51 | + command_line.append(self._launcher_path) |
| 52 | + command_line.extend([self._clang_path] + [ |
| 53 | + '-c', file_paths, '-O3', '-mllvm', '-regalloc-priority-training-log=' |
| 54 | + + log_path, '-mllvm', '-regalloc-enable-priority-advisor=development', |
| 55 | + '-o', output_native_path |
| 56 | + ]) |
40 | 57 |
|
41 |
| - if tf_policy_path: |
42 |
| - command_line.extend(['-mllvm', '-regalloc-priority-model=' + tf_policy_path]) |
43 |
| - compilation_runner.start_cancellable_process(command_line, |
44 |
| - self._compilation_timeout, |
45 |
| - cancellation_manager) |
| 58 | + if tf_policy_path: |
| 59 | + command_line.extend( |
| 60 | + ['-mllvm', '-regalloc-priority-model=' + tf_policy_path]) |
| 61 | + compilation_runner.start_cancellable_process(command_line, |
| 62 | + self._compilation_timeout, |
| 63 | + cancellation_manager) |
46 | 64 |
|
47 |
| - sequence_example = struct_pb2.Struct() |
| 65 | + sequence_example = struct_pb2.Struct() |
48 | 66 |
|
49 |
| - with io.open(log_path, 'rb') as f: |
50 |
| - sequence_example.ParseFromString(f.read()) |
| 67 | + with io.open(log_path, 'rb') as f: |
| 68 | + sequence_example.ParseFromString(f.read()) |
51 | 69 |
|
52 |
| - for key, value in sequence_example.fields.items(): |
53 |
| - e = tf.train.SequenceExample() |
54 |
| - e.ParseFromString(base64.b64decode(value.string_value)) |
55 |
| - print(e) |
56 |
| - if not e.HasField('feature_lists'): |
57 |
| - continue |
58 |
| - r = ( |
59 |
| - e.feature_lists.feature_list['reward'].feature[-1].float_list |
60 |
| - .value[0]) |
61 |
| - if reward_only: |
62 |
| - result[key] = (None, r) |
63 |
| - else: |
64 |
| - del e.feature_lists.feature_list['reward'] |
65 |
| - result[key] = (e, r) |
| 70 | + for key, value in sequence_example.fields.items(): |
| 71 | + e = tf.train.SequenceExample() |
| 72 | + e.ParseFromString(base64.b64decode(value.string_value)) |
| 73 | + print(e) |
| 74 | + if not e.HasField('feature_lists'): |
| 75 | + continue |
| 76 | + r = ( |
| 77 | + e.feature_lists.feature_list['reward'].feature[-1].float_list |
| 78 | + .value[0]) |
| 79 | + if reward_only: |
| 80 | + result[key] = (None, r) |
| 81 | + else: |
| 82 | + del e.feature_lists.feature_list['reward'] |
| 83 | + result[key] = (e, r) |
66 | 84 |
|
67 |
| - finally: |
68 |
| - tf.io.gfile.rmtree(working_dir) |
| 85 | + finally: |
| 86 | + tf.io.gfile.rmtree(working_dir) |
69 | 87 |
|
70 |
| - return result |
| 88 | + return result |
0 commit comments