Skip to content

Feat/conv 1 d #1907

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

Open
wants to merge 6 commits into
base: conv
Choose a base branch
from
Open

Feat/conv 1 d #1907

wants to merge 6 commits into from

Conversation

keshvituteja
Copy link
Collaborator

Added an implementation of the 1D convolution with stride 1.

@keshvituteja keshvituteja requested a review from yhmtsai August 6, 2025 11:36
@ginkgo-bot ginkgo-bot added reg:build This is related to the build system. reg:ci-cd This is related to the continuous integration system. type:matrix-format This is related to the Matrix formats mod:all This touches all Ginkgo modules. labels Aug 6, 2025
@upsj
Copy link
Member

upsj commented Aug 6, 2025

I'm wondering, should this be called a convolution, or rather something like "stencil"? In the context of finite difference methods, convolution may be overly specific, but the same kernel is still useful.

@keshvituteja
Copy link
Collaborator Author

I'm wondering, should this be called a convolution, or rather something like "stencil"? In the context of finite difference methods, convolution may be overly specific, but the same kernel is still useful.

We are planning to use it in context of Machine Learning (through pyGinkgo), so, we decided to name it 'Convolution' for now. I/We are open to the naming scheme

@yhmtsai
Copy link
Member

yhmtsai commented Aug 6, 2025

could you rebase your branch on origin/conv first to reduce the file changes?

@upsj
Copy link
Member

upsj commented Aug 6, 2025

The base commit seems incorrect, this should be based on/merged into develop

@yhmtsai
Copy link
Member

yhmtsai commented Aug 6, 2025

@upsj It is not ready for merging into develop.
we will work on conv after this branch a little bit to check whether the interface/constructor makes sense or not.

const auto kernel_size = kernel.get_size(); // K
const auto* kernel_ptr = kernel.get_const_data(); // pointer to kernel data

for (int i = 0; i < x_size[0]; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

it seems not to be correct.
your formula is x[0] = kernel[0] * b[0];
but it should be x[i] = sum_k kernel[k] * b[i+k], right?

Copy link
Collaborator Author

@keshvituteja keshvituteja Aug 14, 2025

Choose a reason for hiding this comment

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

I updated it according to what torch does, it should work fine now c629e5b

const auto kernel_size = kernel.get_size(); // K
const auto* kernel_ptr = kernel.get_const_data(); // pointer to kernel data

for (int i = 0; i < x_size[0]; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

you need to use size_t to deal from the dense size

Copy link
Collaborator Author

@keshvituteja keshvituteja Aug 14, 2025

Choose a reason for hiding this comment

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

I changed it to size_t in 630a24d, does size_t allow negative values? I am currently using std::ptrdiff_t for negative values

Copy link
Member

Choose a reason for hiding this comment

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

I tend to go by the rule of thumb: if you ever need to use a subtraction (even if you think the values will never become negative), you should use a signed type, so I would agree with using int64 or ptrdiff_t

keshvituteja and others added 3 commits August 14, 2025 12:51
Supports single rhs currently, added an assert statement to ensure that

Co-authored-by: Yu-Hsiang M. Tsai <19565938+yhmtsai@users.noreply.github.com>
@keshvituteja keshvituteja requested a review from yhmtsai August 14, 2025 14:17
throw DimensionMismatch(
__FILE__, __LINE__, __func__, "x", x_rows, 1,
"(b + 2*padding - kernel)/stride + 1",
(b_rows + 2 * 2 - kernel_len) / 1 + 1, 1,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
(b_rows + 2 * 2 - kernel_len) / 1 + 1, 1,
(b_rows + 2 * 0 - kernel_len) / 1 + 1, 1,

shouldn't padding be zero now?

Copy link
Member

Choose a reason for hiding this comment

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

padding equal 2 in your example such that it become numpy result.
IMO, we should follow the default from torch not numpy.
also, if it needs to fit numpy, it should be something like ((x-1)*stride + kernel - b)/2

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The example also matches the torch result. We can change the padding inside the conv_kernels.cpp. It's result would be same as this function:
torch.nn.functional.conv1d(x, w, bias=None, stride=1, padding=2)

We can accept padding in arguments but currently we don't do that, so i hard coded it to be 2. The function also works for zero-padding. For zero padding, it should be similar to:
torch.nn.functional.conv1d(x, w, bias=None, stride=1, padding=0)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I changed it to zero but it will work if change it to any other padding as well

int padding = 2;
int output_length = (x_size[0] + 2 * padding - kernel_size) / stride + 1;

for (gko::size_type i = 0; i < x_size[0]; ++i) {
Copy link
Member

Choose a reason for hiding this comment

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

because you are using substraction later, so I will say you can use gko::int64 directly as Tobias mentioned

Copy link
Member

Choose a reason for hiding this comment

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

also I think start with output will be easier than with input

Comment on lines 45 to 46
static_cast<std::ptrdiff_t>(
j); // calculate the index in b's row based on the current
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
static_cast<std::ptrdiff_t>(
j); // calculate the index in b's row based on the current
static_cast<std::ptrdiff_t>(
j) * stride; // calculate the index in b's row based on the current

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We might not need the multplication with stride here since we already multiply with stride while calculatingstart in line 41:
gko::int64 start =static_cast<gko::int64>(i * stride) - padding;

@keshvituteja keshvituteja requested a review from yhmtsai August 14, 2025 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mod:all This touches all Ginkgo modules. reg:build This is related to the build system. reg:ci-cd This is related to the continuous integration system. type:matrix-format This is related to the Matrix formats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants