Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions tester/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def process_torch_outputs(obj):
write_to_log("paddle_error", self.api_config.config)
raise


if self.api_config.api_name == "paddle.incubate.nn.functional.fused_rms_norm":
paddle_output = paddle_output[0]
elif self.api_config.api_name == "paddle.unique":
Expand Down Expand Up @@ -278,6 +279,7 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
return False
return True

# Forward output check:
if isinstance(paddle_output, paddle.Tensor):
if isinstance(torch_output, torch.Tensor):
if not compare_paddle_and_torch(paddle_output, torch_output):
Expand All @@ -295,6 +297,8 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
torch_output = torch_output.values
if not compare_paddle_and_torch(paddle_output, torch_output):
return
elif (paddle_out is None or paddle_out.size == 0) and torch_output is None:
pass
Comment on lines +300 to +301

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么需要检查 paddle_out.size == 0 呢?

else:
print("[accuracy error]", self.api_config.config, "\n[output type diff error1], ", type(torch_output), flush=True)
write_to_log("accuracy_error", self.api_config.config)
Expand Down Expand Up @@ -322,6 +326,8 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
print("[accuracy error]", self.api_config.config, "\n[output type diff error4]", flush=True)
write_to_log("accuracy_error", self.api_config.config)
return
elif (paddle_item is None or paddle_item.size == 0) and torch_item is None:
continue
elif not isinstance(paddle_item, paddle.Tensor):
print("[not compare]", paddle_item, torch_item, flush=True)
write_to_log("accuracy_error", self.api_config.config)
Expand All @@ -334,6 +340,8 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
if not compare_paddle_and_torch(paddle_item, torch_item):
return

# Forward check now pass.
# Then do paddle backward and backward result check.
if torch_grad_success:
self.is_backward = True
try:
Expand Down Expand Up @@ -380,6 +388,12 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
}:
paddle_out_grads = paddle_out_grads[:2]
torch_out_grads = torch_out_grads[:2]
elif self.api_config.api_name in {
"paddle.incubate.nn.functional.fused_rotary_position_embedding",
}: # Paddle only has 3 outputs/grads Q, K, V
valid_out_num = len([_ for _ in paddle_output if (_ is not None and _.size > 0)])
paddle_out_grads = paddle_out_grads[:valid_out_num]
torch_out_grads = torch_out_grads[:valid_out_num]
elif self.api_config.api_name in {
"paddle.Tensor.fill_diagonal_tensor",
"paddle.diagonal_scatter",
Expand All @@ -406,10 +420,13 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
is_upper = get_arg(self.api_config, 2, 'upper', default=False)
torch_out_grads[1] = torch.triu(torch_out_grads[1]) if is_upper else torch.tril(torch_out_grads[1])

# Backward output check:
if isinstance(paddle_out_grads, paddle.Tensor):
if isinstance(torch_out_grads, torch.Tensor):
if not compare_paddle_and_torch(paddle_out_grads, torch_out_grads):
return
elif (paddle_out_grads is None or paddle_out_grads.size == 0) and torch_out_grads is None:
pass
else:
print("[accuracy error] backward", self.api_config.config, "\n[output type diff error1], ", type(torch_out_grads), flush=True)
write_to_log("accuracy_error", self.api_config.config)
Expand All @@ -427,6 +444,8 @@ def compare_paddle_and_torch(paddle_tensor, torch_tensor) -> bool:
for paddle_item, torch_item in zip(paddle_out_grads, torch_out_grads):
if isinstance(paddle_item, int):
self.np_assert_accuracy(numpy.array(paddle_item), numpy.array(torch_item), atol=self.atol, rtol=self.rtol)
elif (paddle_item is None or paddle_item.size == 0) and torch_item is None:
continue
elif not isinstance(paddle_item, paddle.Tensor):
print("[not compare]", paddle_item, torch_item, flush=True)
write_to_log("accuracy_error", self.api_config.config)
Expand Down
77 changes: 0 additions & 77 deletions tester/api_config/config_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,83 +642,6 @@ def get_padding_offset(bsz, max_seq_len, seq_lens_this_time):
if self.check_arg(api_config, 1, "index"):
x_dim = self.get_arg(api_config, 0, "x").shape[1]
self.numpy_tensor = numpy.random.randint(0, x_dim, size=self.shape)
elif api_config.api_name == "paddle.incubate.nn.functional.fused_rotary_position_embedding":
q_shape = None
k_shape = None
v_shape = None
q_config = self.get_arg(api_config, 0, "q")
if isinstance(q_config, TensorConfig):
q_shape = q_config.shape
k_config = self.get_arg(api_config, 1, "k")
if isinstance(k_config, TensorConfig):
k_shape = k_config.shape
elif k_config is None:
if q_shape:
k_shape = q_shape.copy()
if len(k_shape) >= 3:
k_shape[-2] = 2
else:
k_shape = [2, 8, 2, 8]
new_k = TensorConfig(k_shape, original_dtype)
if len(api_config.args) > 1:
api_config.args[1] = new_k
elif "k" in api_config.kwargs:
api_config.kwargs["k"] = new_k
v_config = self.get_arg(api_config, 2, "v")
if isinstance(v_config, TensorConfig):
v_shape = v_config.shape
elif v_config is None:
if k_shape:
v_shape = k_shape.copy()
elif q_shape:
v_shape = q_shape.copy()
if len(v_shape) >= 3:
v_shape[-2] = 2
else:
v_shape = [2, 8, 2, 8]
new_v = TensorConfig(v_shape, original_dtype)
if len(api_config.args) > 2:
api_config.args[2] = new_v
elif "v" in api_config.kwargs:
api_config.kwargs["v"] = new_v
cos_config = self.get_arg(api_config, 3, "cos_embed")
sin_config = self.get_arg(api_config, 4, "sin_embed")
cos_shape = cos_config.shape if isinstance(cos_config, TensorConfig) else None
sin_shape = sin_config.shape if isinstance(sin_config, TensorConfig) else None
if self.check_arg(api_config, 0, "q"):
self.numpy_tensor = numpy.random.random(self.shape).astype(self.dtype)
elif self.check_arg(api_config, 1, "k"):
if v_shape and len(v_shape) > 1:
self.shape[1] = v_shape[1]
self.numpy_tensor = numpy.random.random(self.shape).astype(self.dtype)
elif self.check_arg(api_config, 2, "v"):
if k_shape and len(k_shape) > 1:
self.shape = k_shape.copy()
self.numpy_tensor = numpy.random.random(self.shape).astype(self.dtype)
elif self.check_arg(api_config, 3, "cos_embed") or self.check_arg(api_config, 4, "sin_embed"):
batch_size = 1
num_heads = 8
head_dim = 8
if q_shape and len(q_shape) >= 4:
batch_size = q_shape[0]
num_heads = q_shape[1]
head_dim = q_shape[3]
embed_shape = [batch_size, num_heads, 1, head_dim]
if self.shape is None or len(self.shape) != 4:
self.shape = embed_shape
self.numpy_tensor = numpy.random.random(self.shape).astype(self.dtype)
elif self.check_arg(api_config, 5, "position_ids"):
batch_size = 1
seq_len = 8
if q_shape and len(q_shape) >= 3:
batch_size = q_shape[0]
seq_len = q_shape[2]
if self.shape is None or len(self.shape) != 2:
self.shape = [batch_size, seq_len]
positions = numpy.zeros(self.shape, dtype=numpy.int64)
for i in range(self.shape[0]):
positions[i] = numpy.arange(self.shape[1])
self.numpy_tensor = positions.astype(self.dtype)
elif api_config.api_name.startswith("paddle.incubate.segment_"):
if self.check_arg(api_config, 1, "segment_ids"):
batch_size = self.get_arg(api_config, 0, "x").shape[0]
Expand Down
22 changes: 19 additions & 3 deletions tester/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,15 @@ def gen_paddle_output_and_output_grad(self, outputs):
if isinstance(outputs, paddle.Tensor):
result_outputs.append(outputs)
elif isinstance(outputs, list) and len(outputs) > 0 and isinstance(outputs[0], paddle.Tensor):
result_outputs = outputs
for output in outputs:
if (output is not None) and (not isinstance(output, paddle.Tensor) or output.size != 0):
result_outputs.append(output)
Comment on lines +481 to +483

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for output in outputs:
if (output is not None) and (not isinstance(output, paddle.Tensor) or output.size != 0):
result_outputs.append(output)
result_outputs = [output for output in outputs
if output is not None and isinstance(output, paddle.Tensor) and output.size != 0]

elif isinstance(outputs, paddle.autograd.autograd.Hessian) or \
isinstance(outputs, paddle.autograd.autograd.Jacobian):
result_outputs.append(outputs[:])
elif isinstance(outputs, tuple):
for output in outputs:
if isinstance(output, paddle.Tensor):
if isinstance(output, paddle.Tensor) and output.size != 0:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是否会影响 0size 的测试?

result_outputs.append(output)
elif isinstance(output, list):
for item in output:
Expand All @@ -500,6 +502,8 @@ def gen_paddle_output_and_output_grad(self, outputs):
isinstance(output[0], paddle.autograd.autograd.Jacobian)):
for lazy_obj in output:
result_outputs.append(lazy_obj[:])
elif output is None or output.size == 0:
continue
else:
raise ValueError("outputs format not support")
# elif isinstance(output, list) and len(output) > 0 and isinstance(output[0], paddle.Tensor):
Expand All @@ -510,6 +514,8 @@ def gen_paddle_output_and_output_grad(self, outputs):
result_outputs_grads = []
if len(self.outputs_grad_numpy) == 0:
for output in result_outputs:
if output is None or output.size == 0:
continue
Comment on lines +517 to +518

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

此处应该是冗余的

dtype = str(output.dtype).split(".")[-1]
if USE_CACHED_NUMPY:
dtype = "float32" if dtype == "bfloat16" else dtype
Expand All @@ -522,6 +528,8 @@ def gen_paddle_output_and_output_grad(self, outputs):
numpy_tensor = (numpy.random.random(output.shape) - 0.5).astype(dtype)
self.outputs_grad_numpy.append(numpy_tensor)
for i, numpy_tensor in enumerate(self.outputs_grad_numpy):
if numpy_tensor is None or numpy_tensor.size == 0:
continue
Comment on lines +531 to +532

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同冗余

dtype = str(result_outputs[i].dtype).split(".")[-1]
result_output_grad = paddle.to_tensor(
numpy_tensor,
Expand Down Expand Up @@ -571,11 +579,15 @@ def gen_torch_output_and_output_grad(self, outputs):
elif isinstance(outputs, torch.Size):
result_outputs.append(torch.tensor(outputs))
elif isinstance(outputs, list) and len(outputs) > 0 and isinstance(outputs[0], torch.Tensor):
result_outputs = outputs
for output in outputs:
if output is not None:
result_outputs.append(output)
Comment on lines +582 to +584

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for output in outputs:
if output is not None:
result_outputs.append(output)
result_outputs = [output for output in outputs if output is not None]

elif isinstance(outputs, tuple):
for output in outputs:
if isinstance(output, torch.Tensor):
result_outputs.append(output)
elif output is None:
continue
else:
raise ValueError("outputs format not support")
# elif isinstance(output, list) and len(output) > 0 and isinstance(output[0], torch.Tensor):
Expand All @@ -586,6 +598,8 @@ def gen_torch_output_and_output_grad(self, outputs):
result_outputs_grads = []
if len(self.outputs_grad_numpy) == 0:
for output in result_outputs:
if output is None:
continue
dtype = str(output.dtype).split(".")[-1]
if USE_CACHED_NUMPY:
dtype = "float32" if dtype == "bfloat16" else dtype
Expand All @@ -598,6 +612,8 @@ def gen_torch_output_and_output_grad(self, outputs):
numpy_tensor = (numpy.random.random(output.shape) - 0.5).astype(dtype)
self.outputs_grad_numpy.append(numpy_tensor)
for i, numpy_tensor in enumerate(self.outputs_grad_numpy):
if numpy_tensor is None:
continue
Comment on lines +615 to +616

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同冗余

dtype = str(result_outputs[i].dtype).split(".")[1]
result_output_grad = torch.tensor(
numpy_tensor,
Expand Down