Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用paddle-gpu 3.0.0.dev20241016的包,paddle.static.nn.fc的输出维度与输入不一致 #68797

Open
gqzhang-ai opened this issue Oct 18, 2024 · 1 comment
Assignees

Comments

@gqzhang-ai
Copy link

bug描述 Describe the Bug

使用Paddle-gpu 20241016的daily包,运行下述代码出现如下报错,使用Paddle-gpu 3.0beta则可以正常运行。
image
测试代码:
import os
import unittest

import numpy as np

import paddle
from paddle import base
from paddle.base import core

SEED = 2021

class TestReduceSumNet(unittest.TestCase):

def set_reduce_sum_function(self, x):
    # keep_dim = False
    return paddle.sum(x, axis=-1)

def _test(self, run_cuda=True):
    main_prog = paddle.static.Program()
    startup_prog = paddle.static.Program()
    main_prog.random_seed = SEED
    startup_prog.random_seed = SEED
    np.random.seed(SEED)

    a_np = np.random.random(size=(2, 3, 4)).astype('float32')
    b_np = np.random.random(size=(2, 3, 4)).astype('float32')
    label_np = np.random.randint(2, size=(2, 1)).astype('int64')

    with paddle.static.program_guard(main_prog, startup_prog):
        a = paddle.static.data(name="a", shape=[2, 3, 4], dtype='float32')
        b = paddle.static.data(name="b", shape=[2, 3, 4], dtype='float32')
        label = paddle.static.data(name="label",
                                   shape=[2, 1],
                                   dtype='int64')

        a_1 = paddle.static.nn.fc(x=a,
                                  size=4,
                                  num_flatten_dims=2,
                                  activation=None)
        b_1 = paddle.static.nn.fc(x=b,
                                  size=4,
                                  num_flatten_dims=2,
                                  activation=None)
        z = paddle.add(a_1, b_1)
        z_1 = self.set_reduce_sum_function(z)

        prediction = paddle.static.nn.fc(x=z_1,
                                         size=2,
                                         activation='softmax')

        cost = paddle.nn.functional.cross_entropy(input=prediction,
                                                  label=label)
        loss = paddle.mean(cost)
        sgd = paddle.optimizer.Momentum(learning_rate=0.01)
        sgd.minimize(loss)

    if run_cuda:
        place = core.CUDAPlace(0)
    else:
        place = paddle.CPUPlace()

    exe = paddle.static.Executor(place)
    exe.run(startup_prog)

    print("Start run on {}".format(place))
    for epoch in range(100):

        pred_res, loss_res = exe.run(main_prog,
                                     feed={
                                         "a": a_np,
                                         "b": b_np,
                                         "label": label_np
                                     },
                                     fetch_list=[prediction, loss])
        if epoch % 10 == 0:
            print("Epoch {} | Prediction[0]: {}, Loss: {}".format(
                epoch, pred_res[0], loss_res))

    return pred_res, loss_res

def test_cuda(self):
    cuda_pred, cuda_loss = self._test(True)
    cpu_pred, cpu_loss = self._test(False)

    self.assertTrue(np.allclose(cuda_pred, cpu_pred))
    self.assertTrue(np.allclose(cuda_loss, cpu_loss))

if name == 'main':
paddle.enable_static()
unittest.main()

其他补充信息 Additional Supplementary Information

No response

@zhangbo9674
Copy link
Contributor

这是框架的一个 bug,已提交修复 PR:https://github.com/PaddlePaddle/Paddle/pull/68806/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants