Skip to content

Commit 575e009

Browse files
authored
Disable view for some torch api tests. (#1567)
* Disable view for some torch api tests. * Optimize the parsing of gpu_time and add the compute of flop, gbs of stack. * Add the compute of flop and gbs for unstack. * Add swish. * Add the compute of flop and gbs for concat and split. * Disable swish for torch. * Fix the setting and use of run_torch.
1 parent e4b5294 commit 575e009

File tree

16 files changed

+132
-57
lines changed

16 files changed

+132
-57
lines changed

api/common/api_param.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ def __init__(self, op_type, params=None):
166166
self.__name = op_type
167167
self.__framework = "paddle"
168168
self.__run_tf = True
169+
self.__run_torch = True
169170
self.api_name = self.name
170171
self.paddle_api = None
171172
self.torch_api = None
@@ -174,7 +175,6 @@ def __init__(self, op_type, params=None):
174175
self.params_list = None
175176
self.backward = False
176177
self.feed_spec = None
177-
self.run_torch = True
178178
self.alias_name = None
179179

180180
@classmethod
@@ -213,6 +213,14 @@ def run_tf(self):
213213
def run_tf(self, value):
214214
self.__run_tf = value
215215

216+
@property
217+
def run_torch(self):
218+
return self.__run_torch
219+
220+
@run_torch.setter
221+
def run_torch(self, value):
222+
self.__run_torch = value
223+
216224
def compute_dtype(self):
217225
dtype = None
218226
for name, value in vars(self).items():
@@ -336,7 +344,7 @@ def __str__(self):
336344
'feed_spec', 'alias_name'
337345
]
338346
if self.framework != "paddle":
339-
exclude_attrs.append("run_torch")
347+
exclude_attrs.append("_APIConfig__run_torch")
340348
exclude_attrs.append("_APIConfig__run_tf")
341349
prefix = ""
342350
debug_str = ('[%s][%s] %s {\n') % (self.framework, self.name,

api/common/benchmark.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ def __init__(self, framework, testing_mode):
2626
self.reset()
2727

2828
def reset(self):
29+
if hasattr(self, "extra_tensors") and self.extra_tensors is not None:
30+
del self.extra_tensors
31+
self.extra_tensors = None
2932
self.feed_list = None
3033
self.fetch_list = None
3134
self._backward = False

api/common/launch.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ def _parse_gpu_time(self, line):
9696
class NsightRunner(object):
9797
def run(self, cmd, profile_from_start=False):
9898
stdout, exit_code = self._nsight(cmd, profile_from_start)
99+
parse_status, gpu_time = self._parse_logs(stdout.split("\n"))
100+
if parse_status:
101+
return gpu_time
99102
if exit_code == 0:
100-
parse_status, gpu_time = self._parse_logs(stdout.split("\n"))
101-
if parse_status:
102-
return gpu_time
103-
print("Running Error:\n {}".format(stdout))
103+
print("Running Error:\n {}".format(stdout))
104104
return 0.0
105105

106106
def _nsight(self, cmd, profile_from_start):
@@ -179,16 +179,20 @@ def _print_logs_in_range(self, logs, line_from, line_to):
179179
print("")
180180

181181
def _parse_gpu_time(self, line):
182-
infos = line.strip().split()
183-
percent = float(infos[0].replace("%", "")) * 0.01
184-
gpu_time = float(infos[1].replace(",", "")) * 1E-6
185-
calls = int(infos[2].replace(",", ""))
186-
function = infos[7]
187-
for i in range(8, len(infos)):
188-
function = function + " " + infos[i]
189-
#print("percent: %.2f; gpu_time: %.4f ms; calls: %d; function: %s" %
190-
# (percent, gpu_time, calls, function))
191-
return gpu_time / percent
182+
try:
183+
infos = line.strip().split()
184+
percent = float(infos[0].replace("%", "")) * 0.01
185+
gpu_time = float(infos[1].replace(",", "")) * 1E-6
186+
calls = int(infos[2].replace(",", ""))
187+
function = infos[7]
188+
for i in range(8, len(infos)):
189+
function = function + " " + infos[i]
190+
#print("percent: %.2f; gpu_time: %.4f ms; calls: %d; function: %s" %
191+
# (percent, gpu_time, calls, function))
192+
return gpu_time / percent
193+
except Exception as e:
194+
sys.stderr.write("Error: parsing \"{}\". {}\n".format(line, e))
195+
return 0.0
192196

193197

194198
class NsightRunnerForDynamicScheduling(object):

api/common/paddle_op_benchmark.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ def __init__(self, testing_mode):
316316
else:
317317
paddle.disable_static()
318318
flags = paddle.get_flags(['FLAGS_use_autotune'])
319-
self.use_autotune = flags['FLAGS_use_autotune']
320-
if self.use_autotune:
319+
self._use_autotune = flags['FLAGS_use_autotune']
320+
if self._use_autotune:
321321
config = {
322322
"kernel": {
323323
"enable": True,
@@ -448,7 +448,7 @@ def _run_main_iter(step=1):
448448
outputs.append(var)
449449
else:
450450
outputs.append(var.numpy())
451-
if self.use_autotune and not self.backward:
451+
if self._use_autotune and not self.backward:
452452
paddle.fluid.core.update_autotune_status()
453453
return outputs
454454

@@ -661,8 +661,3 @@ def build_graph(self, config=None):
661661
self.build_program(config)
662662
self.feed_list = self.feed_vars
663663
self.fetch_list = self.fetch_vars
664-
665-
666-
class PaddleDynamicAPIBenchmarkBase(PaddleOpBenchmarkBase):
667-
def __init__(self):
668-
super(PaddleDynamicAPIBenchmarkBase, self).__init__("dynamic")

api/common/special_op_list.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"less",
8686
"linspace",
8787
"remainder",
88+
"set_value",
8889
"shard_index",
8990
"size",
9091
"truncated_gaussian_random",

api/tests/activation.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self):
2323
self.api_list = {
2424
'sigmoid': 'sigmoid',
2525
'hardsigmoid': 'hardsigmoid',
26+
'swish': 'swish',
2627
'hardswish': 'hardswish',
2728
'softplus': 'softplus',
2829
'tanhshrink': 'tanhshrink',
@@ -39,6 +40,13 @@ def run_tf(self):
3940
return False
4041
return True
4142

43+
@property
44+
def run_torch(self):
45+
if self.api_name in ["swish"]:
46+
print("-- %s is not supported in torch." % self.api_name)
47+
return False
48+
return True
49+
4250

4351
@benchmark_registry.register("activation")
4452
class PaddleActivation(PaddleOpBenchmarkBase):

api/tests/concat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ def build_graph(self, config):
3232
if config.backward:
3333
self.append_gradients(result, xs)
3434

35+
def compute_flop_and_byte(self, config):
36+
forward_flop = 0
37+
forward_byte = 0
38+
for shape in config.x_shape:
39+
forward_byte += numel(shape) * sizeof(config.x_dtype[0]) * 2
40+
if not config.backward:
41+
return forward_flop, forward_byte
42+
else:
43+
# To be implemented.
44+
return None, None
45+
3546

3647
@benchmark_registry.register("concat")
3748
class TorchConcat(PytorchOpBenchmarkBase):

api/tests/expand_as.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def build_graph(self, config):
3636
x = self.variable(name='x', shape=config.x_shape, dtype=config.x_dtype)
3737
y = self.variable(name='y', shape=config.y_shape, dtype=config.y_dtype)
3838
y.requires_grad = False
39-
result = x.expand_as(y)
39+
result = x.expand_as(y).contiguous()
4040

4141
self.feed_list = [x]
4242
self.fetch_list = [result]

api/tests/set_value.py

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,52 @@
1616

1717

1818
@benchmark_registry.register("set_value")
19-
class SetValueConfig(APIConfig):
20-
def __init__(self):
21-
super(SetValueConfig, self).__init__("set_value")
22-
self.run_torch = False
19+
class PaddleSetValue(PaddleOpBenchmarkBase):
20+
def build_graph(self, config):
21+
x = self.variable(
22+
name='x',
23+
shape=config.x_shape,
24+
dtype=config.x_dtype,
25+
stop_gradient=True)
2326

24-
def init_from_json(self, filename, config_id=0, unknown_dim=16):
25-
super(SetValueConfig, self).init_from_json(filename, config_id,
26-
unknown_dim)
27+
if config.is_tensor_value:
28+
value = self.variable(
29+
name='value',
30+
shape=config.value_shape,
31+
dtype=config.value_dtype,
32+
stop_gradient=True)
33+
x[:, 10:500:2] = value
34+
35+
self.feed_list = [x, value]
36+
self.fetch_list = [x]
37+
else:
38+
x[:, 0:20, ::2] = 10000
39+
40+
self.feed_list = [x]
41+
self.fetch_list = [x]
2742

2843

2944
@benchmark_registry.register("set_value")
30-
class PaddleSetValue(PaddleOpBenchmarkBase):
45+
class TorchSetValue(PytorchOpBenchmarkBase):
3146
def build_graph(self, config):
32-
input = self.variable(
33-
name='input', shape=config.input_shape, dtype=config.input_dtype)
34-
input.stop_gradient = True
47+
x = self.variable(
48+
name='x',
49+
shape=config.x_shape,
50+
dtype=config.x_dtype,
51+
stop_gradient=True)
3552

3653
if config.is_tensor_value:
3754
value = self.variable(
3855
name='value',
3956
shape=config.value_shape,
40-
dtype=config.value_dtype)
41-
input[:, 10:500:2] = value
42-
43-
self.feed_list = [input, value]
44-
self.fetch_list = [input]
57+
dtype=config.value_dtype,
58+
stop_gradient=True)
59+
x[:, 10:500:2] = value
4560

61+
self.feed_list = [x, value]
62+
self.fetch_list = [x.contiguous()]
4663
else:
47-
input[:, 0:20, ::2] = 10000
64+
x[:, 0:20, ::2] = 10000
4865

49-
self.feed_list = [input]
50-
self.fetch_list = [input]
66+
self.feed_list = [x]
67+
self.fetch_list = [x.contiguous()]

api/tests/size.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@
1818
@benchmark_registry.register("size")
1919
class PaddleSize(PaddleOpBenchmarkBase):
2020
def build_graph(self, config):
21-
input = self.variable(
22-
name='input', shape=config.input_shape, dtype=config.input_dtype)
23-
result = paddle.fluid.layers.size(input)
21+
x = self.variable(name='x', shape=config.x_shape, dtype=config.x_dtype)
22+
result = paddle.numel(x)
2423

25-
self.feed_list = [input]
24+
self.feed_list = [x]
2625
self.fetch_list = [result]
2726

2827

2928
@benchmark_registry.register("size")
3029
class TorchSize(PytorchOpBenchmarkBase):
3130
def build_graph(self, config):
32-
input = self.variable(
33-
name='input', shape=config.input_shape, dtype=config.input_dtype)
34-
result = torch.numel(input=input)
31+
x = self.variable(name='x', shape=config.x_shape, dtype=config.x_dtype)
32+
result = torch.numel(x=x)
3533

36-
self.feed_list = [input]
34+
self.feed_list = [x]
3735
self.fetch_list = [result]

0 commit comments

Comments
 (0)